当前位置: 移动技术网 > IT编程>脚本编程>Python > python中ASCII码字符与int之间的转换方法

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

2018年08月06日  | 移动技术网IT编程  | 我要评论

不凡的改变第一季,艳鬼追魂,ued黑庄

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之间的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网