In python, the error SyntaxError: unexpected character after line continuation character occurs when the escape character is misplaced in the string or characters added after line continuation character. The “\” escape character is used to indicate continuation of next line. If any characters are found after the escape character “\”, The python interpreter will throw the error unexpected character after line continuation character in python language.
The string in python contains escape sequence characters such as \n, \0, \t etc. If you miss or delete quotes in the string, the escape character “\” in the string is the end of the current line. If any characters are found after the “\” is considered invalid. Python therefore throws this error SyntaxError: Unexpected character after line continuation character.
SyntaxError: unexpected character after line continuation character
Here, we see the common mistakes that causes this SyntaxError: Unexpected character after line continuation character error and how to fix this error. The error would be thrown as like below.
File "/Users/python/Desktop/test.py", line 1
z=\n
^
SyntaxError: unexpected character after line continuation character
[Finished in 0.1s with exit code 1]
Root Cause
The back slash “\” is considered to be th end of line in python. If any characters are added after the back slash is deemed invalid. The back slash “\” is also used as an escape sequence character in the python string. For instance “\n” is a new line character. “\t” is a tab in python.
If the quotation in the string is missing or deleted, the escape character “\” will be treated as the end of the line. The characters added after that will be considered invalid. Therefore, Python throws this SyntaxError: unexpected character after line continuation character error
How to reproduce this error
If any characters are added after the back slash “\”, they will be treated as invalid. Adding the python code after the end of line character “\” will throw this error SyntaxError: unexpected character after line continuation character. In this example, the character “n” in the “\n” is considered invalid.
z=\n
print z
Output
File "/Users/python/Desktop/test.py", line 1
z=\n
^
SyntaxError: unexpected character after line continuation character
[Finished in 0.0s with exit code 1]
Solution 1
The python string should be enclosed with single or double quotes. Check the string that has been properly enclosed with the quotations. If a quotation is missed or deleted, correct the quotation. This is going to solve this error. The code above will be fixed by adding the quotation in “\n”.
z="\n"
print z
Output
[Finished in 0.0s]
Solution 2
If you want to add the “\n” as a character with no escape sequence operation, then “r” should be prefixed along with the string. This will print the characters as specified within the string. The code below will be printed as “\n” instead of a new line character.
z=r"\n"
print z
Output
\n
[Finished in 0.1s]
Solution 3
If a character is added after the end of the line, the character should be moved to the next line. When the copy and paste operation is performed, the new line characters are missing. Fixing the intent of the code will resolve this SyntaxError: unexpected character after line continuation character error.
x = 3
y = 2
if x==3 \ or y==2 :
print x
Output
File "/Users/python/Desktop/test.py", line 3
if x==3 \ or y==2 :
^
SyntaxError: unexpected character after line continuation character
[Finished in 0.0s with exit code 1]
Solution
x = 3
y = 2
if x==3 \
or y==2 :
print x
Output
3
[Finished in 0.0s]
Solution 4
If a white space is added after the end of the “\” line character, it should be removed. The character is not visible in the code. If the python interpreter throws this error and no characters are visible, a whitespace must be availabe after that. Removing all whitespace after the end of the “\” character will resolve this error.
x = 3
y = 2
if x==3 \ --> a space available after \
or y==2 :
print x
Output
File "/Users/python/Desktop/test.py", line 3
if x==3 \ or y==2 :
^
SyntaxError: unexpected character after line continuation character
[Finished in 0.0s with exit code 1]
Solution
x = 3
y = 2
if x==3 \
or y==2 :
print x
Output
3
[Finished in 0.0s]