python - 如何使用Python根据ascii顺序移动字符

标签 python arrays string character ascii

例如,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/

相关文章:

python - 是否可以使用 dask 获取集合的交集?

c - 使用字符数组进行二进制搜索?

c - 将二进制文件构造成数组 C

java - 在 Java 中,如何将单个字段的多个值解析为 String 数组?

python - 无法更改 Django 管理模板

python - 如何在Google Colab上下载TensorFlow训练好的模型?

python - 张量 - 定义函数,同时评估另一个函数

Android日历添加日期

java - 在java中将字母数字字符串转换为字节?

c - 如何标记字符串[数组]?