Reputation: 3
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
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
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