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
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
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"
or use:
print(r'This is \backslash')
But escaping with this \\backslash
is recommended.