字符串移位操作
#coding=utf-8
'''
Author:
主要用于转换字符串,类似凯撒密码的转换
比如:
a右移2个位置,为c
d左移2个位置,则ndigits为-2
y右移2个位置,为a
...
'''
mods = [99, 122]
def shift(strVal, ndigits):
resultVal = []
for c in strVal:
strC = str(c)
if strC.isalpha():
targetAscii = ord(strC)+ndigits # 转换后的字符的ASCII
resultVal.append( chr( targetAscii if targetAscii<=mods[strC.islower()] else targetAscii-26 ) )
else:
resultVal.append(strC)
return ''.join(resultVal)
if __name__=='__main__':
print(ord('a'))
print(ord('y'))
print(ord('z'))
print(ord('A'))
print(ord('Z'))
print(shift('y', 2))
print(shift('Y', 2))
print(shift('map', 2))
print(shift('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.', 2))