Python index of the character in string | Example code

Simple use Python find function or index function to get the index of the character in the string. The difference between the two is what happens when the search string isn’t found. find() returns -1 and index() raises ValueError.

Example get index of a character in a string python

Simple example code.

Using find()

s = 'Position of a character'

res = s.find('s')

print(res)

Using index()

s = 'Position of a character'

res = s.index('s')

print(res)

Output:

Python index of the character in string

Do comment if you have any doubts and suggestions on this Python index topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.