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

yipeiwu_com6年前Python基础

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设计】。

相关文章

Python制作数据导入导出工具

python 2.6编写,自己瞎写的,备用 ''' Export and Import ElasticSearch Data. Simple Example At __mai...

python实现字典(dict)和字符串(string)的相互转换方法

本文实例讲述了python实现string和dict的相互转换方法。分享给大家供大家参考,具体如下: 字典(dict)转为字符串(string) 我们可以比较容易的将字典(dict)类型...

Python金融数据可视化汇总

Python金融数据可视化汇总

通过本篇内容给大家介绍一下Python实现金融数据可视化中两列数据的提取、分别画、双坐标轴、双图、两种不同的图等代码写法和思路总结。 import matplotlib as mpl...

Python中dict和set的用法讲解

Python中dict和set的用法讲解

dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 举个例子,假设要...

python从入门到精通(DAY 1)

python从入门到精通(DAY 1)

1、要点    (1) 在C语言中没有字符串,只有字符,    在python中的字符串hello,在C语言中是以字符数组在内存存放['h','e...