Manan
Manan

Reputation: 189

How can I change a character in a string automatically?

I am trying to make an encrypting program. So far, I have only been able to change the order. How can I make it change a character to another character ? Ex; 'n' => '&', 'a' => '*', etc

I have tried to make a variable array to change the character.

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

Upvotes: 3

Views: 308

Answers (2)

robert
robert

Reputation: 811

If you're just making a simple cipher program (not anything for actual production,) I would suggest converting each letter to to ascii code as seen here http://www.asciitable.com/ by using ord(), shifting by adding a number, and converting back to text with chr().

Upvotes: 0

Shubham
Shubham

Reputation: 351

Can't you just try replace method on string something like

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

For multiple replacing use

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

Upvotes: 1

Related Questions