pythonpositioncharacteralphabet

Get character position in alphabet


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?

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 send it back one step. There might be a better way of doing it, any tips?


Solution

  • It is called index. For example:

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

    Note: in Python 2, string.ascii_lowercase was named string.lowercase.