Python Forum

Full Version: How Do I find Index of a character in string?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how do i find index of each character,

myString = "kitti"
for char in mystring:
    print(index of each letter)
that didnt help at all, only confused me more, didnt see what i was lookiing for. is the question that difficult or long to answer here?
if you have a string 'mystr' for example,
mystr = 'hello zippy'
a string is just a list of characters,
so first character is mystr[0]
print(mystr[0])
will give you a 'h'
print(mystr[1])
will give you a 'e'
you can find index of the z in ziggy with:
idx = mystr.index('zippy')
print(mystr[idx])
will give you a 'z'
mystr = 'hello zippy'
so to find the index value of the second z?
myString = "kitti"
for index, char in enumerate(mystring):
    print(index, char)