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.
-
Are you asking about http://docs.python.org/library/stdtypes.html#id4 ? Are you asking about the find method, specifically? http://docs.python.org/library/stdtypes.html#str.find – S.Lott Mar 23 '09 at 19:00
-
Thanks yeah just the find method. I found that page but no examples. – Joan Venge Mar 23 '09 at 19:03
-
It says -1 on the page. What more did you need to know? – S.Lott Mar 23 '09 at 19:05
-
No I mean, just some examples. – Joan Venge Mar 23 '09 at 20:52
9 Answers
I'm not sure what you're looking for, do you mean find()
?
>>> x = "Hello World"
>>> x.find('World')
6
>>> x.find('Aloha');
-1

- 480,997
- 81
- 517
- 436
-
1
-
2Thanks, but why -1 here, not for index? I thought python prefers exceptions over special return values. – Joan Venge Mar 23 '09 at 19:02
-
-
2find 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. – bobince Mar 23 '09 at 19:35
-
this works great for me - totally forgot that 0 result actually means a match! – Gordon Oct 07 '16 at 14:49
-
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

- 307,395
- 66
- 306
- 293
-
4
-
1it raises the error instead of returning a code, which is more pythonic, in my opinion. – SilentGhost Nov 08 '09 at 11:33
-
7exceptions 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
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

- 14,854
- 11
- 100
- 103

- 19,928
- 7
- 68
- 105
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)

- 32,083
- 17
- 62
- 73
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))

- 360
- 5
- 14
Try
myString = 'abcabc'
myString.find('a')
This will give you the index!!!

- 5,749
- 1
- 30
- 49

- 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
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

- 355
- 3
- 8
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.

- 440
- 1
- 4
- 12