<分区>
当我输入 "abc"
时,我想得到 "bcd"
作为输出。
所以我希望 A
成为 B
并且 B
成为 C
等等直到 Z
将是 A
。
那么我该怎么做呢,我一点头绪都没有。
<分区>
当我输入 "abc"
时,我想得到 "bcd"
作为输出。
所以我希望 A
成为 B
并且 B
成为 C
等等直到 Z
将是 A
。
那么我该怎么做呢,我一点头绪都没有。
最佳答案
您可以使用 translate
直接将字母更改为不同的字母:
try:
from string import makestrans
except ImportError:
maketrans = str.maketrans
from string import ascii_lowercase
#old = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
#new = 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA'
offset = 1
old_lower = ascii_lowercase
new_lower = old_lower[offset:] + old_lower[:offset]
old = old_lower + old_lower.upper()
new = new_lower + new_lower.upper()
# Create a translate table.
trans = maketrans(old, new)
# Translate your string using trans
print("abc".translate(trans))
# bcd
关于python - 将字符串的所有字母加 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22481248/