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
6 Answers
+ 3
If you append the specific characters to a list, you will be able to save them separate from the original string.
+ 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,''))
+ 2
Post your code for us to look at
+ 1
Ah your code really helped, thank you.
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
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 "?"