您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python中的文本移位功能

Python中的文本移位功能

看起来您正在进行cesar密码加密,因此您可以尝试执行以下操作:

strs = 'abcdefghijklmnopqrstuvwxyz'      #use a string like this, instead of ord() 
def shifttext(shift):
    inp = raw_input('Input text here: ')
    data = []
    for i in inp:                     #iterate over the text not some list
        if i.strip() and i in strs:                 # if the char is not a space ""  
            data.append(strs[(strs.index(i) + shift) % 26])    
        else:
            data.append(i)           #if space the simply append it to data
    output = ''.join(data)
    return output

In [2]: shifttext(3)
Input text here: how are you?
Out[2]: 'krz duh brx?'

In [3]: shifttext(3)
Input text here: Fine.
Out[3]: 'Flqh.'

strs[(strs.index(i) + shift) % 26]:线以上手段找到字符的索引istrs,然后添加移位值it.Now,对最终值(索引+偏移)申请%26向得到移位索引。传递给时,此移位索引会strs[new_index] 产生所需的移位字符。

python 2022/1/1 18:29:11 有221人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶