Now lets move on to more advanced operations of strings.
Professor Evans mentioned something called “methods“, and he explains that methods are:
built in procedures provided by python
Well it might be a bit hard to digest this information but let’s think of it this way. Lets pretend you buy a sexy Ducati 1199 Panigale S Tricolore:
Okay lets not get distracted here, back to methods.
Suppose you bought a very nice bike. The primary function is to commute, to travel from one place to another. So in this case, we can say that this Ducati has a function called
"Spin Wheels", well because that is primarily how motorbikes move. How do the wheels spin? You’ll need a function called
"Start Engine". And after you start the engine you can “On the Headlights”. If a driver is road-hogging, you can
"Horn Sexily". And when you are pulled over by the highway police you will need to
"Apply Brakes". These are essentially methods. Methods are like verbs, they are actions you perform on an object, in this case the bike. Methods can be independent, you can
"Apply Brakes" even if you have not
"Start Engine". They can also be inter-related, when you
"Apply Brakes" your
"Spin Wheels" will decrease in effect., or that you can only “On the Headlights” if you have performed
So if we look at strings, when we say strings have methods, in this case the method we are learning is called
"find", we are saying that strings are a type of object that can let us use their method
"find" to search for sequences of characters within themselves.
Professor Evans also mentioned that in Unit two we will learn to define new methods, well for now just that it that you will learn how to teach your Ducati how to reverse.
On a complicated machine like the Ducati, it might be hard you to even
"Get Your Legs Over The Bike". Well in Python not so much, especially with the
.find() method of strings. The way to use this method is such:
ducati = "Authentic Italian performance. Ducati builds emotions."
# this is what I got from Ducati's Company Profile Page
print ducati.find("Italian") # will give you 10
print ducati.find("p") # will give you 18
print ducati.find("ugly") # will give you -1
Now let’s try to understand this. Just like how
"Start Engine" will cause you beast to roar up,
find() will cause the search string, in this case the string is named
ducati, to react. When
ducati finds the string that you are looking for , the target string
"Italian", it will react by giving you an integer, and this integer is the index where the target string first appears in the search string. That is why
"Italian" gives you a reaction of
"p" gives you
18. In programming we call this reaction a return.
So why did the last
find() method return a
-1? The find() method returns a
-1 when the target string cannot be fond in the search string. In the example it just means that
ducati is not
find() has many variations. Just like
"Apply Brakes", it can be a hard braking, or a soft braking, or some where in the middle. When you were using
find() previously, you entered a target string into the parentheses. The string you enter into the parentheses is also called a parameter. Parameters are basically instructions to the methods, controlling how the methods actually work. In the case of
"Apply Brakes", the parameter might be
"Strength", in the form of an integer from
10, softest to hardest. The
find() method must take in at least one parameter, which is the target string, but it can take in another parameter, which is an integer. This integer will determine the position in the search string where
find() will start looking for the target string.
Again from Ducati’s Company Profile page:
ducati = "Ducati is proud to represent Italian industry - the essence of Italian style shows through the design of each bike."
print ducati.find("Italian") # will give you 29
print ducati.find("Italian", 29) # will give you 29
print ducati.find("Italian",30) # will give you 64
The first occurrence of the word
"Italian" is at position
29. When we restrict find to start searching at position 29, the first occurrence of
"Italian" is still
29. However when we restrict
find() to start at position
30, it will see the string as
"talian industry ..." and will not return it. It will however continue searching and eventually find the next
"Italian" at position
64. And what if it doesn’t find the target string? Yes you are right, it returns
We just have two more quizzes and then its Homework 1, feels great huh only one homework for the entire unit!