例如,hello, world
应转换为 ifmmo, xpsme
。 ( a
-> b
, b
-> c
, ..., z
-> a
)
在 C 语言中,可以简单地写 print ch+1;
进行移位。然而,当尝试执行此 Python 时,我得到:
>>> [i+1 for i in "hello, world"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects
最佳答案
看看这个
a = [chr(ord(i)+1) for i in "hello, world"]
print ''.join(map(str,a))
或
for i in "hello, world":
if i.isalpha():
b.append(chr(ord(i)+1))
elif i == ' ' or i == ',':
b.append(i)
print ''.join(map(str,b))
关于python - 如何使用Python根据ascii顺序移动字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40260555/