Examples for string find in Python

PythonStringFind

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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

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

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

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

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

Solution 6 - Python

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.

Solution 7 - Python

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

Solution 8 - Python

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.

Solution 9 - Python

Try

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

This will give you the index!!!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoan VengeView Question on Stackoverflow
Solution 1 - PythonPaolo BergantinoView Answer on Stackoverflow
Solution 2 - PythonSilentGhostView Answer on Stackoverflow
Solution 3 - PythonPhil HView Answer on Stackoverflow
Solution 4 - PythonDanaView Answer on Stackoverflow
Solution 5 - PythonLucianNovoView Answer on Stackoverflow
Solution 6 - PythonMarkusQView Answer on Stackoverflow
Solution 7 - PythonIsrael Alberto RVView Answer on Stackoverflow
Solution 8 - PythonN.ElsayedView Answer on Stackoverflow
Solution 9 - PythonDave BrownView Answer on Stackoverflow