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)
Asked By: FATCAT47

||

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

Answered By: Xetera

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)
Answered By: Hashem
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.