Examples for string find in Python
Solution 1
you can use str.index
too:
>>> 'sdfasdf'.index('cc')
Traceback (most recent call last):
File "<pyshell#144>", line 1, in <module>
'sdfasdf'.index('cc')
ValueError: substring not found
>>> 'sdfasdf'.index('df')
1
Solution 2
I'm not sure what you're looking for, do you mean find()
?
>>> x = "Hello World"
>>> x.find('World')
6
>>> x.find('Aloha');
-1
Solution 3
From the documentation:
str.find(sub[, start[, end]])
Return the lowest index in the string where substring sub is found within the slice
s[start:end]
. Optional arguments start and end are interpreted as in slice notation. Return-1
if sub is not found.
So, some examples:
>>> my_str = 'abcdefioshgoihgs sijsiojs '
>>> my_str.find('a')
0
>>> my_str.find('g')
10
>>> my_str.find('s', 11)
15
>>> my_str.find('s', 15)
15
>>> my_str.find('s', 16)
17
>>> my_str.find('s', 11, 14)
-1
Solution 4
Honestly, this is the sort of situation where I just open up Python on the command line and start messing around:
>>> x = "Dana Larose is playing with find()"
>>> x.find("Dana")
0
>>> x.find("ana")
1
>>> x.find("La")
5
>>> x.find("La", 6)
-1
Python's interpreter makes this sort of experimentation easy. (Same goes for other languages with a similar interpreter)
Solution 5
If you want to search for the last instance of a string in a text, you can run rfind.
Example:
s="Hello"
print s.rfind('l')
output: 3
*no import needed
Complete syntax:
stringEx.rfind(substr, beg=0, end=len(stringEx))
Comments
-
Joan Venge almost 2 years
I am trying to find some examples but no luck. Does anyone know of some examples on the net? I would like to know what it returns when it can't find, and how to specify from start to end, which I guess is going to be 0, -1.
-
Joan Venge about 15 yearsThanks, what do you get, if it can't find?
-
Joan Venge about 15 yearsThanks, but why -1 here, not for index? I thought python prefers exceptions over special return values.
-
Joan Venge about 15 yearsThanks SilentGhost, I used your method.
-
Amit Patil about 15 yearsfind and index are the same functionality but with different results on no match. Python might in general prefer exceptions, but many users expect there to be a non-exception-raising-find-index method as well, especially as that's how it's done in almost every other language.
-
endolith over 14 yearsIPython makes this sort of thing even better.
-
endolith over 14 yearsWhy would you choose one over the other?
-
SilentGhost over 14 yearsit raises the error instead of returning a code, which is more pythonic, in my opinion.
-
Asela Liyanage almost 14 yearsexceptions should not be used for flow control. so, only use index() if no match would be abnormal.
-
SilentGhost almost 14 years@Wahnfrieden: using exception is perfectly pythonic. And certainly doesn't deserve a downvote.
-
Rafael Almeida over 12 years@aehlke in fact, in Python, using exceptions to control flow is usual and even recommended: stackoverflow.com/questions/6092992/…
-
Gordon over 7 yearsthis works great for me - totally forgot that 0 result actually means a match!
-
Pooja Khatri about 4 yearscan we use or here for ex- x.find("Welcome" or "welcome")
-
Exelian over 3 yearsHi @Dave Brown, did you test your code? I think you'll find that it doesn't perform as indented because you made a typo