pythonstringintaddition

how to take a number and find its alphabet position python


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;

Solution

  • Use the chr() and ord() functions.

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