64

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.

nihiser
  • 604
  • 1
  • 15
  • 28
Joan Venge
  • 315,713
  • 212
  • 479
  • 689

9 Answers9

113

I'm not sure what you're looking for, do you mean find()?

>>> x = "Hello World"
>>> x.find('World')
6
>>> x.find('Aloha');
-1
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
45

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
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • 4
    Why would you choose one over the other? – endolith Nov 08 '09 at 01:46
  • 1
    it raises the error instead of returning a code, which is more pythonic, in my opinion. – SilentGhost Nov 08 '09 at 11:33
  • 7
    exceptions should not be used for flow control. so, only use index() if no match would be abnormal. – aehlke Jul 05 '10 at 11:19
  • 10
    @Wahnfrieden: using exception is perfectly pythonic. And certainly doesn't deserve a downvote. – SilentGhost Jul 05 '10 at 12:44
  • 8
    @aehlke in fact, in Python, using exceptions to control flow is usual and even recommended: http://stackoverflow.com/questions/6092992/why-is-it-easier-to-ask-forgiveness-than-permission-in-python-but-not-in-java – Rafael Almeida Jan 09 '12 at 20:32
32

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
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Phil H
  • 19,928
  • 7
  • 68
  • 105
17

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)

Dana
  • 32,083
  • 17
  • 62
  • 73
6

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))
LucianNovo
  • 360
  • 5
  • 14
2

find( sub[, start[, end]])

Return the lowest index in the string where substring sub is found, such that sub is contained in the range [start, end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

From the docs.

MarkusQ
  • 21,814
  • 3
  • 56
  • 68
1

Try

myString = 'abcabc'
myString.find('a')

This will give you the index!!!

Exelian
  • 5,749
  • 1
  • 30
  • 49
Dave Brown
  • 31
  • 4
  • 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 – Exelian Sep 30 '20 at 19:15
0

Try this:

with open(file_dmp_path, 'rb') as file:
fsize = bsize = os.path.getsize(file_dmp_path)
word_len = len(SEARCH_WORD)
while True:
    p = file.read(bsize).find(SEARCH_WORD)
    if p > -1:
        pos_dec = file.tell() - (bsize - p)
        file.seek(pos_dec + word_len)
        bsize = fsize - file.tell()
    if file.tell() < fsize:
        seek = file.tell() - word_len + 1
        file.seek(seek)
    else:
        break
0

if x is a string and you search for y which also a string their is two cases : case 1: y is exist in x so x.find(y) = the index (the position) of the y in x . case 2: y is not exist so x.find (y) = -1 this mean y is not found in x.

N.Elsayed
  • 440
  • 1
  • 4
  • 12