15
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

joaquin
  • 82,968
  • 29
  • 138
  • 152
zjm1126
  • 63,397
  • 81
  • 173
  • 221

7 Answers7

19

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
Daniel G
  • 67,224
  • 7
  • 42
  • 42
11
print ('s' in a)     # False
print ('1234' in a)  # True

Use find if you need the index as well, but don't want an exception be raised.

print a.find('s')    # -1
print a.find('1234') # 0
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
5

you can use in operator if you just want to check whether a substring is in a string.

if "s" in mystring:
   print "do something"

otherwise, you can use find() and check for -1 (not found) or using index()

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
5

str.find() and str.index() are nearly identical. the biggest difference is that when a string is not found, str.index() throws an error, like the one you got, while str.find() returns -1 as others' have posted.

there are 2 sister methods called str.rfind() and str.rindex() which start the search from the end of the string and work their way towards the beginning.

in addition, as others have already shown, the in operator (as well as not in) are perfectly valid as well.

finally, if you're trying to look for patterns within strings, you may consider regular expressions, although i think too many people use them when they're overkill. in other (famous) words, "now you have two problems."

that's it as far as all the info i have for now. however, if you are learning Python and/or learning programming, one highly useful exercise i give to my students is to try and build *find() and *index() in Python code yourself, or even in and not in (although as functions). you'll get good practice traversing through strings, and you'll have a better understanding as far as how the existing string methods work.

good luck!

wescpy
  • 10,689
  • 3
  • 54
  • 53
  • Regular Expressions a Overkill?! Why? There's probably no more efficient and clear way of programming most string matching algorithms. – Pedro Rolo Feb 28 '11 at 15:44
  • 1
    For simple searches that can be done with tools like find() and index(), regular expressions are not as good a choice because they tend to be difficult to read without careful inspection. It's rare to be able to skim past a regexp and catch all the details of what it's trying to do. They can be very efficient for complex patterns but maintainability can be an issue. – Mark R. Wilkins Sep 05 '13 at 16:04
1
def substr(s1, s2):  # s1='abc' , s2 = 'abcd'
    i = 0
    cnt = 0
    s = ''
    while i < len(s2):
        if cnt < len(s1):
            if s1[cnt] == s2[i]:
                cnt += 1
                s += s2[i]
            else:
                cnt = 0
                s = ''
        i += 1
    if s == s1:
        print(f'{s1} is substring of {s2}')
    else:
        print(f'{s1} is not a substring of {s2}') 
0

This is the correct way of checking whether a substring is present in a string or not.

def sea(str,str1):

if(str1[0:] in str):

    return "yes"
else:
    return "no"
print(sea("sahil","ah"))
vegetarianCoder
  • 2,762
  • 2
  • 16
  • 27
0

Let's try this according to below mentioned.

method 1:

if string.find("substring") == -1:
 print("found")
else:
 print("not found")

Method 2:

 if "substring" in "string":
  print("found")
 else:
   print("not found")
UdAy Reddy
  • 39
  • 2