Remove all quotations within a string
Question:
- I just want to remove all quotation marks from a string.
- I’ve tried using
.strip()
and a for loop and that doesn’t work either.
- What am I doing wrong?
- I know this question is similar to a few others on stack overflow, I looked at them, and they didn’t give me a straight answer.
string = """Hi" how "are "you"""
string.replace('"',"")
print(string)
Answers:
Strings are immutable in python, string.replace
can’t mutate an existing string so you need to do
string = """Hi" how "are "you"""
new_string = string.replace('"',"")
print(new_string)
Or reassign the existing variable with the new value
There you go.
string = """Hi" how "are "you"""
string = string.replace('"','')
print(string)
or (prefer this method to get rid of unwanted characters.)
string = """Hi" how "are "you"""
for i in range(0, len(string)):
string = string.replace('"','')
print(string)
- I just want to remove all quotation marks from a string.
- I’ve tried using
.strip()
and a for loop and that doesn’t work either. - What am I doing wrong?
- I know this question is similar to a few others on stack overflow, I looked at them, and they didn’t give me a straight answer.
string = """Hi" how "are "you"""
string.replace('"',"")
print(string)
Strings are immutable in python, string.replace
can’t mutate an existing string so you need to do
string = """Hi" how "are "you"""
new_string = string.replace('"',"")
print(new_string)
Or reassign the existing variable with the new value
There you go.
string = """Hi" how "are "you"""
string = string.replace('"','')
print(string)
or (prefer this method to get rid of unwanted characters.)
string = """Hi" how "are "you"""
for i in range(0, len(string)):
string = string.replace('"','')
print(string)