您的位置:首页 > 编程语言 > Python开发

python中ASCII码字符与int之间的转换方法

2018-07-09 14:08 811 查看

ASCII码转换为int:ord('A') 65

int转为ASCII码:chr(65) 'A'

题目内容:

实现一个凯撒密码的变种算法,对输入字符串进行加解密处理

把字母a-z分别循环对应为相距13个位置的字母n-m,即

原文字母:a b c d e f g h i j k l m n o p q r s t u v w x y z

对应字母:n o p q r s t u v w x y z a b c d e f g h i j k l m

大写字母对应方式与小写字母类似,其他符号(含标点符号)不作处理

输入格式:

一个英文字符串

输出格式:

经过上述算法加密的字符串

输入样例:

The Zen of Python

输出样例:

Gur Mra bs Clguba

时间限制:2000ms内存限制:128000kb

题解:string类型无法被修改,若修改需要先转为列表类型,最后再连接起来

str=input()
strlist=list(str)
for i in range(len(strlist)):
if strlist[i]>='a' and strlist[i]<='z':
if ord(strlist[i])+13<=122:
strlist[i]=chr(ord(strlist[i])+13)
else:
strlist[i]=chr((ord(strlist[i])+13)%122+96)
elif strlist[i]>='A' and strlist[i]<='Z':
if ord(strlist[i])+13<=90:
strlist[i]=chr(ord(strlist[i])+13)
else:
strlist[i]=chr((ord(strlist[i])+13)%90+64)
print("".join(strlist))

以上这篇python中ASCII码字符与int之间的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python ASCII int