0

Question about python strings

Is there a way to remove a certain character from a string and store it as a variable? I know how to remove characters from a string without knowing if they'll be in the string, I just don't know how to save the character

12th Jan 2021, 10:06 AM
Daniel Tennant
Daniel Tennant - avatar
6 Réponses
+ 3
If you append the specific characters to a list, you will be able to save them separate from the original string.
12th Jan 2021, 10:13 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
This might be one way of doing it: text = "this is a specific test for a question asked in Q&A" character = input() or "e" removed = [] for i in text: if i == character : removed.append(i) print(removed) print(text.replace(character,''))
12th Jan 2021, 10:25 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Post your code for us to look at
12th Jan 2021, 10:55 PM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Ah your code really helped, thank you.
12th Jan 2021, 11:14 AM
Daniel Tennant
Daniel Tennant - avatar
0
punc = ["?", ".", "!"] for i in punc: speech = speech.replace(i, "") speech = speech.lower() That's what I have to remove them. The speech variable is user inputted and the idea is to jumble the input. The code here takes out punctuation but I'm not sure how to append the specific punctuation that the user has inputted into a list so I can add the removed punctuation to the end. Not sure if I'm making sense
12th Jan 2021, 10:18 AM
Daniel Tennant
Daniel Tennant - avatar
0
Ah now I'm having trouble with the variable being equal to one value or another as it can only call the first value. punc = "?" or "!" or "." or "," When I use the punc variable to check if any of these chars are in the string so I can remove them and then append them to the end of the string it only works for the "?"
12th Jan 2021, 8:48 PM
Daniel Tennant
Daniel Tennant - avatar