- [Speaker] In this video, we are going to take a look
at various capabilities for searching for sub-strings
within a string.
And to do that, we are going to work
with the string called "sentence"
which I've already defined here in snippet number one.
Now let's start with the count method,
which simply counts how many occurrences
of a given sub-string are located within the string.
So if you look up above here,
you can see that there are two occurrences of the word "to"
and we do indeed get "2" as the result
of calling "count" in this case.
Now this might be used for example if you were
doing some basic statistics on the words in a corpus
that contains a lot of text.
Now in this case, it searched the entire string,
but you do have the ability to search a subset
of the string as well.
So if I provide a second argument,
that's the starting index location
where the search should begin.
So effectively, what we get as a result of this
is a slice of the sentence string
starting from index position 12
and then the search or the count occurs
only from that position until the end of the string.
Now clearly, the first word "to" begins at index zero.
So because 12 is after that, this "to" will not be included.
So zero, one, two, three, four, five, six,
seven, eight, nine, 10, 11, 12.
So 12 is the position of this space before the second "to"
and therefore we get "1" as the result.
And as you might expect,
not only can you specify the starting point,
but you can also specify the end point as well.
And this will result in a slice of the original string
starting from index 12 up to but not including
index position 25.
And in this case there is one copy of the word "to"
within that range as well.
Now if you would like to know the physical location
at which a sub-string starts within a string,
you can use the index method for that capability.
So in this case we're looking for the word "be"
which starts at index position three.
Now of course, there is two of those in the original string.
So as you can see, this method finds
only the first occurrence.
If you were looking for more occurrences,
let's say if you were implementing a find capability
within a word processor,
then you would have to start the next search
from the index position one higher
than where the previous search began.
So in this case, we see that "be" is at index three.
There's also the ability to search backwards
by using rindex.
And rindex, in this case we'll give me 16,
which is the starting character position
of the second value "be" within the string.
So it searches backwards until it finds it
and then gives the starting index location
of the word within the string.
Now by the way, both the index and rindex methods
are going to cause a value error
if the value is not found within the string.
So if you would like to instead
get the value minus one to indicate not found,
instead of using index and rindex
you can use find and rfind, as well.
Now in addition to methods, we also have the ability
to simply test for containment.
So here's an expression that just
looks for the sub-string "that"
to see if it's anywhere within the string sentence.
And of course, it is.
So we should see "True" for this one.
And it is case-sensitive.
It is a lexicographical comparison.
So if I use capital that, I get "False"
because I have only lowercase letters
in the original string.
And as you might expect, if I change that one to "not in",
which we introduced previously as well,
"that" with all uppercase letters
is not in the original string
and therefore we get "True" for that particular expression.
Now you also have the ability to search for sub-strings
both at the beginning and at the end of a string.
So for instance, if I use this expression
"sentence.startswith('to')"
it clearly does start with "to"
so we get "True" for that one.
But if I go ahead and change that to "be"
which it does not start with,
indeed, we get "False".
And we can see that this string ends with "question".
So let's go ahead and do an endswith also here.
So "sentence.endswith('question')" is "True"
and if I shorthand that and say, "Does it end with quest?"
well it ends with the full word "question"
so "quest" is not all the way at the end of the string
and therefore we get "False" in that case.