When writing Python code, backslash (“\”) can be used in two instances – as a line continuation character on the code or as an escape character when printing output on stdout, or when writing text into a file. In the first case, the backslash is used to break a long line of Python string, dictionary, set, and lists (maybe there’s more). For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
str1 = "This is a long sentence\ that needs to be broken\ down into multiple\ lines using backslash." print("str1 -->", str1) lst1 = ["Smith", None, "Alice", 46\ ,"Ammon", [-99], "NazarethkirchStraße"] print("lst1-->", lst1) dict1 = {"Name":None, "Name1": "Ammon",\ "Reg": 2719, "Salary": "44K", "YOB": 1990} print("dict1-->", dict1) set1 = {24, 57, 78, 98,\ 77, -9, 0, 45} print("set1-->", set1) |
Output (on the stdout (standard output)):
As you have seen, we broke the long lines of code and got no errors. Notice, however, that the “\” character did not break the output on the stdout. The only thing that happened was text wrapping – which is the inherent property of most code editors.
Note: Backslash cannot be used to break a long number, nor can it be used to break variables.
When using the backslash as a line continuation character, things can go wrong, and we end up with “Unexpected Character After Line Continuation Character SyntaxError“. Here are some common causes and solutions to this error.
A. Using backslash as a division operator
The division operator is represented by a forward slash (“/”) in Python and not “\”. Using the backslash for division leads to the SyntaxError.
1 |
sph = 1800\24 |
Output:
SyntaxError: unexpected character after line continuation character
A. Putting any Character after the line continuation character
When used as a line continuation character, backslash cannot be followed by any character – not even white space.
1 2 |
str1 = "This is a long sentence "\a "that needs to be broken." |
Output:
SyntaxError: unexpected character after line continuation character
1 2 |
str1 = "This is a long sentence "\ # there's a white space after the backslash "that needs to be broken." |
Output:
SyntaxError: unexpected character after line continuation character
Python did not expect the character “a” after the backslash in the first example and the invisible white space in the second example. In this case, the solution to the error is to remove the characters coming after the line continuation character, as shown below for example 1.
1 2 3 4 |
str1 = "This is a long sentence "\ "that needs to be broken." print("str1 -->", str1) |
Output:
str1 --> This is a long sentence that needs to be broken.
So far, we have discussed how to break long lines of code but what if we want to break long lines on the printout of the stdout or when writing text into a file? That is where we can use the backslash (“\”) with other characters as an escape sequence.
Using backslash as an escape character
A backslash can be used as part of a special character sequence to convey a unique message to the Python interpreter. Examples of the special sequences include “\n” for a new line, “\t” for a tab character, and “\r” is a carriage return. To go to a new line in the stdout or when writing text into a file, we need to use “\n”.
Note: When using “\” as a line continuation operator, it can be placed outside the quotes. However, it must be inside the quotes when we use it in a special sequence.
1 2 3 4 |
str1 = "This is a long sentence\n that needs to be broken." str2 = "The new line can br\neak a line anywhere" print(str1) print(str2) |
Output (on the stdout):
The example below shows a long sentence we want to write into a file. In the first case, we write the whole text as it is, and in the second case, we write the string into a text file in a chunk of 5 words. At the end of the 5 words in each line, we use the “\n” character to move into a new line.
Truncated output (dummy_long.txt):
Truncated output (dummy.txt):
As shown in the output, dummy_long.txt contains the text in one line (don’t be confused by text wrapping done by the text editor), whereas, for the second case, each line in the text file contains only 5 words.
Conclusion
The Python error “Unexpected Character After Line Continuation Character SyntaxError” happens when we wrongly use the line continuation character (backslash). That can happen in two ways: first, if we use the backslash “\” for division instead of forward-slash (“/”), and second when we add a character after the backslash- even a white space.
There should be no character after “\” when it is used as a line continuation character. We have also seen that backslash can be used as part of a special sequence. In those cases, the special sequence must occur inside the quotes of a string. We showed how to use a new line character (“\n”) to break lines on the stdout and write text into a file.