Search This Blog

Python: get the index of a char in a string

find and index method are available. The difference is that find returns -1 when what you're looking for isn't found, index throws an exception.
s='abc'
print(s.index('b')) # 1
print(s.find('b')) # 1
print(s.find('d')) # -1
print(s.index('d')) # exception

No comments:

Post a Comment