-1

I have following text output as a str():

"We\'ve ... here\'s why ... That\'s how ... it\'s"

As you see, there's always a "" before the apostrophe.

Then, I tried following:

    text = text.replace("\", "")
                                              
SyntaxError: EOL while scanning string literal

Same happened with text.replace("\\", "") or text.strip("\").

What can I do to get rid of all \ in my text?

Thanks!

Solution:

\' is the way how Python outputs strings. Once you do print() or export the string, there's no issue.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Christopher
  • 2,120
  • 7
  • 31
  • 58

1 Answers1

1

In Python 2.7.13, this code:

text = "We\'ve ... here\'s why ... That\'s how ... it\'s"
text = text.replace("\\", "")
print text

outputs We've ... here's why ... That's how ... it's

Are you using a different version of Python, or do you have a specific section of code to look at?

Edit:

I also wanted to mention that the apostrophe's have a backslash before them because it's an escape character. This essentially just means that you're telling the terminal that python is outputting to to interpret the apostrophe literally (in this situation), and to not handle it differently than any other character.

It's also worth noting that there are other good uses for backslashes (\n, or newline is one of the most useful imho).

For example:

text = "We\'ve ... here\'s why ...\nThat\'s how ... it\'s"
print text

outputs:

We've ... here's why ...
That's how ... it's

Not that the \n is interpreted as a request for the terminal to go to a newline before interpreting the rest of the string.

DeathCamel57
  • 92
  • 10