You can use str.translate and str.maketrans
>>> s = 'mypassword'
>>> s.translate(s.maketrans('iamBo', '!@M8.'))
'[email protected]'
Answer from Jab on Stack OverflowStrange results. There is a lab where we are to take input and alter the input.
word = input()
password = ''
i = 0
while i < len(word):
if word[i] == 'i':
password = password + '1'
elif word[i] == 'a':
password = password + '@'
elif word[i] == 'm':
password = password + 'M'
elif word[i] == 'B':
password = password + '8'
elif word[i] == 's':
password = password + '$'
else:
password == password + word[i]
i = i + 1
password = password + '!'
print(password)This code 'works' but it is dropping the items on in the while loop.
Check this out, the code below works as expected:
word = input()
password = ''
i = 0
while i < len(word):
if word[i]=='i':
password +='1'
elif word[i]=='a':
password +='@'
elif word[i]=='m':
password +='M'
elif word[i]=='B':
password +='8'
elif word[i]=='s':
password +='$'
else:
password += word[i]
i +=1
password = password + '!'
print(password)The only difference is using the shorter += way of making a variable equal to itself and the other item.
output from top code is
mypassword
M@$$!
Output from bottom code is
mypassword
Myp@$$word!
:O!!!! What gives? What am I missing?
Hello, everyone! I have a question. I'm a beginner in Python and I am currently taking an introduction to Python class. One of our assignments this week is to create a password Modifier.
Here's the assignment.
Many user-created passwords are simple and easy to guess. Write a program that takes a simple password and makes it stronger by replacing characters using the key below, and by appending "!" to the end of the input string.
-
i becomes 1
-
a becomes @
-
m becomes M
-
B becomes 8
-
s becomes $
Ex: If the input is:
mypassword
the output is:
Myp@$$word!
I managed to pass the assignment but I wasn't able to include the append function. I am assuming it wants me to store the values inside a list and then append the '!' at the end. I am unsure however to store each item inside a list.
Here's what I came up with.
word = input()
password = ''
for i in word:
password = ''
if i == 'i':
password += '1'
print(f'{password}', end='')
continue
elif i == 'a':
password += '@'
print(f'{password}', end='')
continue
elif i == 'm':
password += 'M'
print(f'{password}', end='')
continue
elif i == 'B':
password += '8'
print(f'{password}', end='')
continue
elif i == 's':
password += '$'
print(f'{password}', end='')
continue
else:
print(f'{i}', end='')
print(f'!', end='')
What could I have done differently?
Sorry for the long post. I just want to try and learn as much as I can.
You can use str.translate and str.maketrans
>>> s = 'mypassword'
>>> s.translate(s.maketrans('iamBo', '!@M8.'))
'[email protected]'
You can also harcode the shifting letters. Answers below are better but you can go like:
new_pass = ''
for char in password:
if char == 'i':
new_pass += '!'
elif char == 'a':
new_pass +='@'
elif char == 'm':
new_pass +='M'
.
.
.
else:
new_pass += char
You can use .replace()
word = 'mypassword'
word = word.replace('i','!')
word = word.replace('a','@')
word = word.replace('m','M')
word = word.replace('B','8')
word = word.replace('o','.')
word = word + 'q*s'
print(word)
>>> [email protected]*s
You can also put all the .replace() on one line,
word = word.replace('i','!').replace('a','@').replace('m','M').replace('B','8').replace('o','.')
Following your original logic, you need to initialise a word to return, otherwise all characters get appended to your old password.
word = input()
password = ''
#checking for and modifying user's password if containing the characters: i, a, m, B, or o
for character in word: # update password rather than word
if(character=='i'): #if user's password contains the letter i change to !
password += '!'
elif(character=='a'): #if user's password contains the letter a change to @
password += '@'
elif(character=='m'): #if the user's password contains the letter m change to M
password += 'M'
elif(character=='B'): #if the user's password contains the letter B change to 8
password += '8'
elif(character=='o'): #if the user's password contains the letter o change to .
password += '.'
else: # add else statement for when symbols do not get replaced
password += character
print(password)