Often, we want to look into a string and find a substring. For example if we were presented a series of lines formatted as follows:
Sat Jan 5 09:14:16 2008
and we wanted to pull out only the second half of the address (i.e.,
uct.ac.za) from each line, we can do this by using the
find method and string slicing.
First, we will find the position of the at-sign in the string. Then we will find the position of the first space after the at-sign. And then we will use string slicing to extract the portion of the string which we are looking for.
>>> data = 'From firstname.lastname@example.org Sat Jan 5 09:14:16 2008' >>> atpos = data.find('@') >>> print(atpos) 21 >>> sppos = data.find(' ',atpos) >>> print(sppos) 31 >>> host = data[atpos+1:sppos] >>> print(host) uct.ac.za >>>
We use a version of the
find method which allows us to specify a position in the string where we want
find to start looking. When we slice, we extract the characters from "one beyond the at-sign through up to but not including the space character".
The documentation for the
find method is available at