how to take a number and find its alphabet position python

german picture german · May 25, 2017 · Viewed 9.4k times · Source

So far, I have been able to take a letter and find it's number position. Example, a == 0 But now I want to take that number, and add 4. Then return the alphabet equivalent of that number. So, a == 0; 0 + 4 = 5; should return f. Cause that is fifth in the alphabet! Here is my code so far:

def convert_int(str):
    a = string.lowercase.index(str)
    addition = a +13
    return addition;

Answer

Pedro von Hertwig Batista picture Pedro von Hertwig Batista · May 25, 2017

Use the chr() and ord() functions.

print(ord('a'))  # 97
print(chr(97 + 4))  # e
print(chr(ord('f') + 2))  # h