user378010
user378010

Reputation: 3

How to convert numbers into their corresponding chronological english letters in python

i.e. 1->A and 26->z

I'm working on a program that is supposed to turn a letter into it's corresponding number, change the number, and then convert it back to a new letter. I've set up a dictionary that allows me to go forwards, but I am unable to convert the number back to a letter, can anyone help?

Upvotes: 0

Views: 96

Answers (2)

Dan
Dan

Reputation: 577

Try chr(i + 96):

>>> print(*(chr(i + 96) for i in range(1, 27)))
a b c d e f g h i j k l m n o p q r s t u v w x y z

Upvotes: 1

MSeifert
MSeifert

Reputation: 152735

You could use a dictionary that has the number as key and the letter as value. Then conversion can be done with [num]:

>>> import string

>>> translate_dict = dict(zip(range(1, 27), string.ascii_lowercase))

>>> translate_dict[2]
'b'

Upvotes: 1

Related Questions