如何取一个数字并找到它的字母位置 python

how to take a number and find its alphabet position python

到目前为止, 我已经能够拿一封信并找到它的号码位置。例如,a == 0 但现在我想取那个数字,然后加 4。然后 return 那个数字的字母等价物。 所以, a == 0; 0 + 4 = 5; 应该 return f。因为它在字母表中排在第五位! 到目前为止,这是我的代码:

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

使用 chr()ord() 函数。

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

简单的方法,你可以使用ord将ascii字符转换为十进制,例如:A~65,然后你可以添加你想要的任何数字,并使用chr 将其转换回 ascii。示例:

chr((ord(a) + number) % 123)

在更难和流行的情况下,你应该在python

中使用dict
import string

alphabet=string.ascii_lowercase
#alphabet='abcdefghijklmnopqrstuvwxyz'

#print char by position: ex: 4 
 print(alphabet[4])
#This will print e