欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-02-02 17:52:51
ASCII码转换为int:ord('A') 65 int转为ASCII码:chr(65) 'A' 题目内容: 实现一个凯撒密码的变种算法,对输入字符串进行加解密处理...

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