Skip to main content
 首页 » 编程设计

python之字母表中的下一个字母

2024年11月24日11webabcd

我在 cs circles 问题 Next Letter 上卡住了如何让 Z 变成 A

x =input() 
 
x=x.upper() 
 
x=chr(ord(x) + 1) 
 
print(x) 

如何让 z 变成 A?

请您参考如下方法:

使用 chr 和 ord:

def next_alpha(s): 
    return chr((ord(s.upper())+1 - 65) % 26 + 65) 
 
for s in 'abcdefghijklmnopqrstuvwxyz': 
    print('%s --> %s' % (s, next_alpha(s))) 
 
a --> B 
b --> C 
... 
y --> Z 
z --> A