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

时间:2022-08-26 12:45:59

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

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

输入格式:

一个英文字符串

输出格式:

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

输入样例:

?
1
The Zen of Python

输出样例:

?
1
Gur Mra bs Clguba

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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之间的转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/winycg/article/details/78093728