-3
  • 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
  • 17,284
  • 1
  • 38
  • 50
FATCAT47
  • 42
  • 9

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
  • 43,885
  • 25
  • 111
  • 113
Xetera
  • 1,125
  • 1
  • 7
  • 14
1

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
  • 39
  • 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. – FishingCode Jul 31 '20 at 23:45