printable - python string character index




Get character position in alphabet (4)

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?


I've only just started learning Python, so I have no idea how efficient this is compared to the other methods, but it works. Also, it doesn't matter whether the text is upper case, lower case or if there is any punctuation etc.

If you want to change all letters:

from string import maketrans

textin = "abcdefghijklmnopqrstuvwxyz"
textout = "cdefghijklmnopqrstuvwxyzab"
texttrans = maketrans(textin, textout)

text = "qcc, gr umpiq"

print text.translate(texttrans)

Also works to change some characters:

from string import maketrans

textin = "81972"
textout = "Seios"
texttrans = maketrans(textin, textout)

text = "811, 9t w7rk2"

print text.translate(texttrans)

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.


You can use ord() to get a character's ASCII position, and chr() to convert a ASCII position into a character.

EDIT: Updated to wrap alphabet so a-1 maps to z and z+1 maps to a

For example:

my_string  = "zebra"
difference = -1
new_string = ''.join((chr(97+(ord(letter)-97+difference) % 26) for letter in my_string))

This will create a string with all the characters moved one space backwards in the alphabet ('ydaqz'). It will only work for lowercase words.


# define an alphabet
alfa = "abcdefghijklmnopqrstuvwxyz"

# define reverse lookup dict
rdict = dict([ (x[1],x[0]) for x in enumerate(alfa) ])

print alfa[1] # should print b

print rdict["b"] # should print 1

rdict is a dictionary that is created by stepping through the alphabet, one character at a time. The enumerate function returns a tuple with the list index, and the character. We reverse the order by creating a new tuple with this code: ( x[1], x[0]) and then turn the list of tuples into a dictionary. Since a dictionary is a hash table (key, value) data structure, we can now look up the index of any alphabet character.

However, that is not what you want to solve your problem, and if this is a class assignment you would probably get 0 for plagiarism if you submit it. For encoding the strings, first create a SECOND alphabet that is organised so that alfa2[n] is the encoded form of alfa[n]. In your example, the second alphabet would be just shifted by two characters but you could also randomly shuffle the characters or use some other pattern to order them. All of this would continue to work with other alphabets such as Greek, Cyrillic, etc.





alphabet