How to handle no element found in String index() Method with Python

1 Answer

0 votes
s = "Python programming language"
 
# print(s.index('C++')) # ValueError: substring not found
 
try :
    print(s.index('C++'))
except ValueError:
    print('Not found')
 
 
 
'''
run:
 
Not found
 
'''
 

 



answered Jan 25, 2025 by avibootz
edited Jan 25, 2025 by avibootz
...