-4
  • 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)
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
FATCAT47
  • 40
  • 10

2 Answers2

5

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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Xetera
  • 1,390
  • 1
  • 10
  • 17
0

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)
Hashem
  • 37
  • 4
  • 2
    A single `.replace()` call will replace all replacement characters. Why call it multiple times? Additionally, you can simplify to `for _ in string`. But again, it’s not needed. – S3DEV Jul 31 '20 at 23:40
  • 3
    the repeated `.replace()` is not *required*. the string gets checked for all quotation marks and replaces with no quotes with only the first line. – de_classified Jul 31 '20 at 23:45