Online Tutorials & Training Materials | STechies.com
Register Login

SyntaxError: unexpected character after line continuation character in Python

11 Jun 2020 9:09 am || 1000

A common error in Python is the "SyntaxError: unexpected character after line continuation character". This usually occurs when the compiler finds a character that is not supposed to be after the line continuation character.

Let us understand this a little more by looking at an example.

Error Code:

print("10 Divided by 2: ", 10\2)

Output:

print("10 Divided by 2: ", 10\2)
^
SyntaxError: unexpected character after line continuation character

SyntaxError: unexpected character after line continuation character

Explanation:

In the code written above, you can observe that the SyntaxError is raised. The code tries to divide 10 by 2 and print the result. But instead of using the division operator, that is the front slash "/", the backslash "\" is used. So, the code is unable to divide the two numbers.

As the backslash is called the line continuation character in Python, it cannot be used for division. Python expects only newline characters or whitespaces after it. So, when it encounters an integer, it throws the error.

Correct code:

print("10 Divide by 2: ", 10/2)

In this code, the frontslash "/" is used for division. So the SyntaxError: unexpected character after line continuation character is avoided here. Hence, the code will run successfully.    


Comments


View previous comments