[no audio]
Friends here
we are going to see three more different operations on your
given string.
They are like 'count', 'index', and 'find' operations.
See now you can easily understand 'count' operation. 'count'
is nothing but just so you know that you're having some
string in that given string a particular character,
how many times it is appearing in a given string, for that
you're using 'count' operation.
Let me open my terminal here
also, I mean Python Shell, Python command line.
Let's say x = "python is easy and it is popular language"
'x.count'. Right.
I'm going to count how many times letter, sorry word
"is" is there, or I am going to count only particular character.
No problem, you can check it.
[no audio]
Right. Or you can check "t", or you can check letter "a".
Right. You can check particular character or some word also.
Let's say I am going to check.
[no audio]
"easy", anyway "easy"
word is there only one time, so how many times that particular
word or character is appearing in a given string, to count
that you are using 'count' operation, right. Now 'x.index',
this is next operation.
Let's say I want to find the index of letter "p".
See very first
letter "p" is there, very first character "p" is there, we know
whenever you assign string in this way, we have positive
indexes as well as negative indexes. If we count with the
positive, letter "p" is at 0,
but at the same time somewhere you have "p", here and here, but
your 'index' will give by default, whatever the 'index' operation
you have on a given string that will look from left to right,
and very first wherever you are getting "p" that index it will
give. But in case if you want to look from particular position,
I want to look from index 1. That means you are skipping index
0. You are not searching from index 0. You're searching
from index 1 to last. Yes, from there 25th you have. Now instead of
25th I am looking from 26, then 27 we have one more "p" letter.
After that do we have anything?
Let's say I am searching from 28.
You don't have that string in a given, after 28th index
that's why I'm getting an error.
Right. Not only that you can check with like "is". So let
me check with from starting, yes somewhere in 7th index
"is" word is starting.
"i", index is 7. But if you try to look after some particular
index, I want to look from 23, after 23rd index
you don't have "is" word
that's why it is giving an error.
That's why you know if particular thing is there in a given string
you can go and search with 'index' to get the index value of
your given string.
But anyway 'index' operation is sometimes you know, it is giving
bad result in this way.
[no audio]
Right. So what I can do is 'x.find', suppose I am doing "is", or
let's say only single character "p",
[no audio]
I want to look from second index, yes somewhere 25th.
I want to look from 26th
index, yes somewhere 27,
I mean "p" letter is there zeroth position, 25th position, 27th
position. I want to look from 28th.
We are getting -1 means, now after 28th
you don't have any "p" letter in a given string. See compared
to 'index', right,
you can simply forget about your 'index' operation,
just better to remember only 'find' operation. Of course, you have
both the things but it's better to remember 'find' operation
to find index of your particular string, or a character.
So how 'find' is going to work? If that particular character
or word is there it will give the index of that, or it will give -1
if it is not there. Suppose after 28th index, from 28th
index up to last you don't have any "p" letter, "p" character
in a given string that's why it is giving -1. Now if this
result is -1, then you can say that this particular string,
this particular character is not present from 28th to last
index, or some so and so index to last. Let's say in entire string
I'm looking for letter called "z", or string character called
"z". You don't have "z" in the given string,
because your string is like 'x' you don't have any where
"z" in this string,
that's why 'x.find' will give -1.
Right. So guys this 'find' is very, very helpful in your real
time. Instead of 'count',
right, of course 'count' is also useful but 'find' is very, very
useful. Let's say simply give me one example, I am trying
to find Java version through my Python script, right?
So you got some output, something like "java version",
some 1.6, something like that, right. First, what I will do
is your 'java_version.find'. First of all, I
will check whether 'Java' is there or not in a given string.
If it is there I will get some non-zero value,
I mean a positive index value. Then I can say that yes, successfully
I found Java and some Java version is there in the variable
called 'java_version'.
Let's say I got an output like while finding my Java version
"Error while finding".
[no audio]
Now if I try to look 'Java' in the 'java_version'
variable, you don't have. That means you are unable
to find your Java version with the help of your logic.
Somewhere something is wrong.
Right. So to take some decision this value will be helpful.
Okay. So just remember guys how to use 'find', and how it is going
to work. While writing our scripts we will use this so that
you will be clear how to use 'find' operation.
Okay. So this is very, very important in your real time.
[no audio]
Okay guys, thank you for watching this video.
[no audio]