Get character position in alphabet

qwerty picture qwerty · May 8, 2011 · Viewed 81.2k times · Source

I'm 90% sure there is a built in function that does this.

I need to find the position of a character in an alphabet. So the character "b" is position 1 (counting from 0), etc. Does anyone know what the function is called?

Thanks in advance!

EDIT: What i'm trying to do is to send all the characters X amount of "steps" back in the alpha bet, so if i have a string with "hi" it would be "gh" if i sent it back one step. There might be a better way of doing it, any tips?

Answer

Senthil Kumaran picture Senthil Kumaran · May 8, 2011

It is called index. For e.g.

>>> import string
>>> string.lowercase.index('b')
1
>>> 

Note: in Python 3, string.lowercase has been renamed to string.ascii_lowercase.