如何自动更改字符串中的字符?

时间:2018-12-20 04:14:42

标签: python python-3.x

我正在尝试制作一个加密程序。到目前为止,我只能更改顺序。如何使其将一个字符更改为另一个字符? 例如; '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

2 个答案:

答案 0 :(得分:1)

您不能只是尝试对字符串进行替换方法 像

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

多次替换使用

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

答案 1 :(得分:0)

如果您只是编写一个简单的密码程序(对于实际生产而言不算什么),我建议使用ord()将每个字母转换为ascii代码,如此处http://www.asciitable.com/所示,并通过加一个数字进行平移,然后使用chr()转换回文本。

相关问题