SyntaxError: unexpected character after line continuation character

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.

  1. Using backslash as division operator in Python
  2. Adding any character right after the escape character
  3. 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")
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python String Isidentifier()

Python String isidentifier()

Table of Contents Hide isidentifier() Syntaxisidentifier() Parametersisidentifier() Return ValueExample 1: How isidentifier() works? Example 1: How to use isidentifier() method? The Python String isidentifier() method is a built-in function that…
View Post
How To Fix Keyerror In Pandas

How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…
View Post
Python String Ljust()

Python String ljust()

Table of Contents Hide ljust() Syntaxljust() Parameters ljust() Return valueExample 1 : Python program to left justify a stringExample 2: ljust() Method With * fillcharExample 3: Returns original string if the…
View Post
Python - List Files In A Directory

Python – List Files in a Directory

Table of Contents Hide Method 1: Using os.listdir() methodMethod 2: Using os.walk() methodMethod 3: Using os.scan() methodMethod 4: Using glob module There are several modules available in Python to list…
View Post
Python String Strip()

Python String strip()

Table of Contents Hide strip() Syntaxstrip() Parametersstrip() Return ValueExample: Working of the strip() methodExample 2 – How to use strip() method in real world? The Python String strip() method is…
View Post