SyntaxError: unexpected character after line continuation character
Srinivas Ramakrishna
Posted on September 6, 2021
ItsMyCode |
In Python, SyntaxError: unexpected character after line continuation character occurs when you misplace the escape character \
inside a string or characters that split into multiline.
The backslash character "\"
is used to indicate the line continuation in Python. If any characters are found after the escape character, the Python interpreter will throw SyntaxError: unexpected character after line continuation character.
SyntaxError: unexpected character after line continuation character.
Sometimes, there are very long strings or lines, and having that in a single line makes code unreadable to developers. Hence, the line continuation character "\"
is used in Python to break up the code into multiline, thus enhancing the readability of the code.
Example of using line continuity character in Python
message = "This is really a long sentence " \
"and it needs to be split acorss mutliple lines " \
"to enhance readibility of the code"
print(message)
# Output
This is really a long sentence and it needs to be split acorss mutliple lines to enhance readibility of the code
As you can see from the above example, it becomes easier to read the sentence when we split it into three lines.
Fixing unexpected character after line continuation character
Let’s take a look at the scenarios where this error occurs in Python.
- Using backslash as division operator in Python
- Adding any character right after the escape character
- Adding new line character in a string without enclosing inside the parenthesis
Also read IndentationError: unexpected indent
Using backslash as division operator in Python
Generally, new developers tend to make a lot of mistakes, and once such is using a backslash \
as a division operator, which throws Syntax Error.
# Simple division using incorrect division operator
a= 10
b=5
c= a\b
print(c)
# Output
File "c:\Projects\Tryouts\listindexerror.py", line 11
c= a\b
^
SyntaxError: unexpected character after line continuation character
The fix is pretty straightforward. Instead of using the backslash *\
* replace it with forward slash operator */
* as shown in the below code.
# Simple division using correct division operator
a= 10
b=5
c= a/b
print(c)
# Output
2
Adding any character right after the escape character
In the case of line continuity, we escape with *\
* and if you add any character after the escaper character Python will throw a Syntax error.
message = "This is line one \n" \+
"This is line two" \
"This is line three"
print(message)
# Output
File "c:\Projects\Tryouts\listindexerror.py", line 1
message = "This is line one \n" \+
^
SyntaxError: unexpected character after line continuation character
To fix this, ensure you do not add any character right after the escape character.
message = "This is line one \n" \
"This is line two \n" \
"This is line three"
print(message)
# Output
This is line one
This is line two
This is line three
Adding any character right after the escape character
If you are using a new line character while printing or writing a text into a file, make sure that it is enclosed with the quotation "\n"
. If you append \n
, Python will treat it as an escape character and throws a syntax error.
fruits = ["Apple","orange","Pineapple"]
for i in fruits:
print(i+\n)
# Output
File "c:\Projects\Tryouts\listindexerror.py", line 3
print(i+\n)
^
SyntaxError: unexpected character after line continuation character
To fix the issue, we have replaced \n
with "\n"
enclosed in the quotation marks properly.
fruits = ["Apple","orange","Pineapple"]
for i in fruits:
print(i+"\n")
The post SyntaxError: unexpected character after line continuation character appeared first on ItsMyCode.
Posted on September 6, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.