python中的字母转换

无意之间了解到了一个很逗比的网站:http://www.pythonchallenge.com/

 

于是就遇到了第二题,他列出了一串文本:

g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.
然后给出了下图:

 

通过图片中的信息我们能得知,其实就是将当前字母往后移动两位。
首先我先用了chr() ord()来转换:

输出结果:

i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.

 

居然告诉我要用 string.maketrans() 函数。于是我尝试了一下,发现这个函数并不存在。网上搜了一下,因为我用的python3.x,string 包已经被整合了,所以就变成了如下代码:

 
test_str = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."

intab = "abcdefghijklmnopqrstuvwxyz"
outtab = "cdefghijklmnopqrstuvwxyzab"
trantab = str.maketrans(intab, outtab)

# print(test_str.translate(trantab))
print(test_str.translate(trantab))

输出结果:

i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.

 
然而还是不知道怎么改url。。。。。。
 
 
 
posted @ 2018-08-01 10:35  阿狸君  阅读(1532)  评论(0编辑  收藏  举报