首页 > 解决方案 > 如何自动更改字符串中的字符?

问题描述

我正在尝试制作一个加密程序。到目前为止,我只能更改顺序。我怎样才能让它改变一个角色到另一个角色?前任; 'n' => '&'、'a' => '*' 等

我试图制作一个变量数组来更改字符。

def convert():
    print('This will re-order your password')
    y = input('Your password? 13 letters, numbers & symbols:    ')
    l = y[0] + y[11] + y[10] + y[9] + y[8] + y[7] + y[6] + y[5] + y[4] + 
y[3] + y[2] + y[1] + y[12]
    print(l)


def original():
    print('This will re-order your password to its original state')
    z = input('Your muddled password? 13 letters, numbers & symbols:    ')
    t = z[0] + z[11] + z[10] + z[9] + z[8] + z[7] + z[6] + z[5] + z[4] + 
z[3] + z[2] + z[1] + z[12]
    print(t)


p = input('Convert or Convert to original? For \'Convert\', type \'c\' or 
\'C\' and for \'Convert to original\', type \'o\' or \'O\':    ')

if p == 'c' or 'C':
    convert()
elif p == 'o' or 'O':
    original()
else:
    print('Invalid')

#  Example Passwords:
#  6539HopPop
#  And@457654321

标签: pythonpython-3.x

解决方案


你不能尝试在字符串上替换方法,比如

password="Imback4u";
newpassword=password.replace(old char,new char)

多次更换使用

password.replace("value1", "").replace("value2", "text")

推荐阅读