Find the Position of One String Inside Another
You need to find the position of one string within another.
Use the find Python function.
For example, to find the starting position of the string def within the string abcdef ghi, you would use:
>>> s = “abcdefghi” >>> s.find(“def”) 3 >>> |
Note that the character positions start at 0, so a position of 3 means the fourth character in the string.
Discussion
If the string you’re looking for doesn’t exist in the string being searched, then find returns the value -1.
See Also
The replace function is used to both find and then replace all occurrences of a string (Recipe 5.16).