Examples for string find in Python

376,418

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))
Share:
376,418
Joan Venge
Author by

Joan Venge

Professional hitman.

Updated on July 08, 2022

Comments

  • Joan Venge
    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
    Joan Venge about 15 years
    Thanks, what do you get, if it can't find?
  • Joan Venge
    Joan Venge about 15 years
    Thanks, but why -1 here, not for index? I thought python prefers exceptions over special return values.
  • Joan Venge
    Joan Venge about 15 years
    Thanks SilentGhost, I used your method.
  • Amit Patil
    Amit Patil about 15 years
    find 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
    endolith over 14 years
    IPython makes this sort of thing even better.
  • endolith
    endolith over 14 years
    Why would you choose one over the other?
  • SilentGhost
    SilentGhost over 14 years
    it raises the error instead of returning a code, which is more pythonic, in my opinion.
  • Asela Liyanage
    Asela Liyanage almost 14 years
    exceptions should not be used for flow control. so, only use index() if no match would be abnormal.
  • SilentGhost
    SilentGhost almost 14 years
    @Wahnfrieden: using exception is perfectly pythonic. And certainly doesn't deserve a downvote.
  • Rafael Almeida
    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
    Gordon over 7 years
    this works great for me - totally forgot that 0 result actually means a match!
  • Pooja Khatri
    Pooja Khatri about 4 years
    can we use or here for ex- x.find("Welcome" or "welcome")
  • Exelian
    Exelian over 3 years
    Hi @Dave Brown, did you test your code? I think you'll find that it doesn't perform as indented because you made a typo