EN
Python - find character index in string
0 points
In this article, we would like to show you how to find a character index in string in Python.
Quick solution:
xxxxxxxxxx
1
text = 'ABC'
2
index = text.find('A')
3
4
print(index) # A
In this example, we use the String find()
method to find indexes of the characters in a given string.
xxxxxxxxxx
1
text = 'ABC'
2
3
indexA = text.find('A')
4
indexB = text.find('B')
5
indexZ = text.find('Z')
6
7
print(indexA) # 0
8
print(indexB) # 1
9
print(indexZ) # -1
Output:
xxxxxxxxxx
1
0
2
1
3
-1