4.3 Accessing characters
Valid indices for strings range from the negative string length to one less than the string length.
guitars = "Fender Gibson Taylor"
guitars[0]
'F'
guitars[-1]
'r'
guitars[-5]
'a'
Use find to locate a substring within an index range in a
string. If my_string
is a string, then find can
have from one to three arguments.
my_string.find(substring, start_index=0, end_index=len(my_string))
This notation means that one argument is required, which is the substring for which you are looking. If you give no other arguments, find starts looking at index 0. If you do not give the third argument, it defaults to the length of the string.
guitars.find("o")
11
guitars.find("o", 12)
18
guitars.find("o", 2, 9)
-1
If Python cannot locate the substring, find returns...