Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find whether a string is contained in another string

Tags:

python

string

a='1234;5'
print a.index('s')

the error is :

> "D:\Python25\pythonw.exe"  "D:\zjm_code\kml\a.py" 
Traceback (most recent call last):
  File "D:\zjm_code\kml\a.py", line 4, in <module>
    print a.index('s')
ValueError: substring not found

thanks

like image 905
zjm1126 Avatar asked Apr 07 '10 06:04

zjm1126


1 Answers

Try using find() instead - this will tell you where it is in the string:

a = '1234;5'
index = a.find('s')
if index == -1:
    print "Not found."
else:
    print "Found at index", index

If you just want to know whether the string is in there, you can use in:

>>> print 's' in a
False
>>> print 's' not in a
True
like image 103
Daniel G Avatar answered Nov 09 '22 10:11

Daniel G