-
Title:
01-50 Finding Strings in Strings
-
Description:
01-50 Finding Strings in Strings
-
So I want to introduce one more operation on strings, which
-
we'll find very useful, which is the Find operation. It gives
-
us the way in a big string to find some sub-string
-
that we're looking for. The way we use Find is a little
-
different from the way we've used other operators so far. Because
-
Find is actually a method, and what that means is it's
-
a built in procedure provided by Python. We'll be able to
-
define our own procedures soon, we'll get to that in unit two.
-
Find is a procedure that operates on strings, so we use it by having
-
a string followed by .find, followed by
-
a parentheses, then we pass in another string.
-
Which is the string that we want to find in
-
the first string. And the output of Find is the
-
position in the string where that sub-string is found, the
-
first occurrence of the string. So, if that string happens
-
to occur in more places than one in the
-
input string, the result of find is always going to give
-
us the position. That's the number where the first occurrence
-
of the sub-string occurs. So the output of using Find
-
will be the first position in the search string, which
-
is this blue string right here, where the target string,
-
which is the purple string, occurs. So that will be
-
a number. If the target string is not a found anywhere
-
in the search string, then the output would be negative
-
1. So let's try a few examples to understand how
-
that works and we'll do this in the Python interpreter.
-
Here I've initialized the variable Pythagoras to hold the string here
-
that's been attributed to Pythagoras. We don't know if
-
he really said it. But it says there's a geometry
-
in the humming of strings, there is music in
-
the spacing of spheres. So now, we have that variable
-
initialized, so I'm going to invoke Find, using Pythagoras as
-
the string that we're searching in, and that's the value
-
that we initialized it to with a string, passing in
-
as the search string the string string. When we run
-
this, we see that we get 40 as the result. If we counted, this is position 0, we
-
would see string starting at position 40. Since I
-
don't want to count that far, we can use our indexing
-
to see if that's right. So let's print Pythagoras
-
starting from index 40, we could print all the
-
way to the end using a colon. And, when
-
we run that, we see that it starts with string
-
which is what we found with the Find. We can search
-
for other positions if we search for Pythagoras the single letter T.
-
Well that matches the beginning, so we should find the resulted position
-
0 which is what we get and we can look for sphere.
-
[BLANK_AUDIO]
-
That will match sphere at the end. We get
-
position 86. Let's print the quote from position 86. And
-
we see the end of the quote starting from sphere. [SOUND]
-
If we search for a string that's not in the string
-
that we're using as the search string, so let's look for
-
say, algebra, which was not in the quote from Pythagoras, we
-
get the output negative one. That means the string was not found.