Making letters from user input disappear from a string

user12739323

Would anyone be able to help me with something simple but I seem to be running into a small snag.

I need to take an input from user for a random string. I then need to ask the user which letters they would like me to make disappear from their string.

I then need to use a for loop to make those specific letters disappear from the string and print showing the missing letters.

I have the following code so far as below:

user_input = input("Please enter a string")
disappear = input("now please enter the chracters you would like to disappear")
for a in user_input.replace(disappear, " "):
    print (a)

My code runs but prints out every letter on a new line and does make my chosen letters disappear but it makes other letters disappear as well.

Any help in the simplest form will be greatly appreciated. Thank you

Nathan

You can do this by replacing each individual letter:

user_input = input("Please enter a string")
disappear = input("now please enter the chracters you would like to disappear")
for a in disappear :
    user_input = user_input.replace(a, '')
print(user_input)

The reason your original script wasn't working was because of this part here:

for a in user_input.replace(disappear, " "):
    print (a)

What happens precisely? Well, first you evaluate:

user_input.replace(disappear, " ")

Say my user_input is 'hello world' and dissapear is 'he'. This would evaluate as 'llo world'. Now what happens:

for a in 'llo world':
    print(a)

And you see that it should print:

l
l
o

w
o
r
l
d

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

hardship with input from user and replace letters

Making multiple objects from user input

Remove certain letters from input string

Changing a string from user input

Remove letter from an ArrayList of letters each time it is found in input of user

How to limit input from user to numbers and letters only in C++

Converting user input to an array of characters, and filtering letters from other characters?

php String from user input and from database

python comparing parentheses from user input and making sure they have a pair

Making a function to get input from user with discord.py?

Making array from user input (Seperated by linebreaks) with Javascript

Pyramid from letters in string

How to take any input from the users in capital letters even if the user is entering it in small letters?

Making a list of separate words with spaces in between from a string input

Making a string from a listbox

Making a table from a string

Get string length from user input

Distinguishing if int or char / string from user input

Compare value of string to input from user

Use inequality from user input string

Read float or string from user input

Compare string with user input from skype chat

Cant return a string from user input in function

Remove characters from a string upon user input

User input to select from a string array

Block user from entering letters

Making a feature matrix from frequency of letters in a DF cell (split string into list of characters in a DF and count)

Python - Making an user input a concatenated string

Prevent inside contents inside parent container from making the parent disappear