1

I am using Python 3 and am trying to find a ways to search for a way to insert a '\' (single backslash) into my program.

I am gettin this error: SyntaxError: EOL while scanning string literal

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
user3751099
  • 31
  • 1
  • 1
  • 3

2 Answers2

3

You have to escape the backslash:

\\

From Python Docs:

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

Also, as @Torxed mentioned in his answer, you can use the prefix r or R:

String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences.

r"Some string with \ backslash"
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
3

or use:

print(r'This is \backslash')

But escaping with this \\backslash is recommended.

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • this does not work see it prints `0.60\textbackslash pm0.073` I want `0.60\pm0.073` – Charlie Parker Nov 17 '21 at 17:21
  • @CharlieParker I think you missunderstood the answer. `print(r"You want 0.60\pm0.073")`. You shouldn't add the string `backslash` there. – Torxed Nov 18 '21 at 13:11