syntaxerror: "unexpected character after line continuation character in python" math
I am having problems with this Python program I am creating to do maths, working out and so solutions but I’m getting the syntaxerror: «unexpected character after line continuation character in python»
this is my code
My problem is with \1.5 I have tried \1.5 but it doesn’t work
Using python 2.7.2
7 Answers 7
The division operator is / , not \
The backslash \ is the line continuation character the error message is talking about, and after it, only newline characters/whitespace are allowed (before the next non-whitespace continues the «interrupted» line.
Outside of a string, a backslash can only appear in this way. For division, you want a slash: / .
If you want to write a verbatim backslash in a string, escape it by doubling it: «\\»
Почему возникает данная ошибка при запуске любого скрипта питон?

Сергей Горностаев,
такая была ошибка, но уже не актуально.

SyntaxError: unexpected character after line continuation character
У тебя в строке где-то болтается символ \ вне строковой константы.
Знак деления не перепутал, случаем?
9 Examples of Unexpected Character After Line Continuation Character (Python)
Here’s everything about the Python syntax error unexpected character after line continuation character:
This error occurs when the backslash character \ is used incorrectly.
So if you want to learn all about this Python error and how to solve it, then you’re in the right place.
SyntaxError: Unexpected Character After Line Continuation Character in Python

So you got SyntaxError: unexpected character after line continuation character?—don’t be afraid this article is here for your rescue and this error is easy to fix:
Syntax errors are usually the easiest to solve because they appear immediately after the program starts and you can see them without thinking about it much. It’s not like some logical rocket science error.
However, when you see the error SyntaxError: unexpected character after line continuation character for the first time, you might be confused:
What Is a Line Continuation Character in Python?
A line continuation character is just a backslash \—place a backlash \ at the end of a line, and it is considered that the line is continued, ignoring subsequent newlines.
You can use it for explicit line joining, for example. You find more information about explicit line joining in the official documentation of Python. Another use of the backslash \ is to escape sequences—more about that further below.
However, here is an example of explicit line joining:
So as you can see the output is: This is a huge line. It is very large, but it needs to be printed on the screen in one line. For this, the backslash character is used. No line breaks.
The backslash \ acts like glue and connects the strings to one string even when they are on different lines of code.
When to Use a Line Continuation Character in Python?
You can break lines of code with the backslash \ for the convenience of code readability and maintainability:
The PEP 8 specify a maximum line length of 79 characters—PEP is short for Python Enhancement Proposal and is a document that provides guidelines and best practices on how to write Python code.
However, you don’t need the backslash \ when the string is in parentheses. Then you can just do line breaks without an line continuation character at all. Therefore, in the example above, you didn’t need to use the backslash character, as the entire string is in parentheses ( … ).
However, in any other case you need the backslash \ to do a line break. For example (the example is directly from the official Python documentation):
Why all that talking about this backslash? Here is why:
The error SyntaxError: unexpected character after line continuation character occurs when the backslash character is incorrectly used. The backslash is the line continuation character mentioned in the error!
Examples of “SyntaxError: Unexpected Character After Line Continuation Character” in Python
Here are examples of the character after-line continuation character error:
Example #1
The error occurs when you add an end-of-line character or line continuation character as a list item:
Easy to fix—just put the backslash \ in quotes:
Example #2
You want to do a line break after printing something on the screen:
Easy to fix, again—the backslash \ goes in to quotes:
Perhaps you want to add a new line when printing a string on the screen, like this.
Example #3
Mistaking the slash / for the backslash \—the slash character is used as a division operator:
Another easy syntax error fix—just replace the backslash \ with the slash /:
Example #5
However, when a string is rather large, then it’s not always that easy to find the error on the first glance.
Here is an example from Stack Overflow:
The line of code contains several backslashes \—so you need to take a closer look.
Split the line of code into logical units and put each unit on separate line of code. This simplifies the debugging:
Now you can easily see where the backslash \ is missing—with a sharp look at the code itself or through the debugger output. A backslash \ misses after the 1.5 in line of code #4.
So let’s fix this:
Example #6
Another common case of this error is writing the paths to Windows files without quotes. Here is another example from Stack Overflow:
The full path to the file in code of line #1 must be quoted “”. Plus, a colon : is to be placed after the drive letter in case of Windows paths:
The Backslash \ as Escape Character in Python
The backslash \ is also an escape character in Python:
Use the backslash \ to escape service characters in a string.
For example, to escape a tab or line feed service character in a string. And because the backslash \ is a service character on its own (remember, it’s used for line continuation), it needs to be escaped too when used in a string—\\.
This is why in the last example the path contains double backslashes \\ instead of a single backslash \ in line of code #1.
However, you don’t need any escapes in a string when you use a raw string. Use the string literal r to get a raw string. Then the example from above can be coded as:
No backslash \ escapes at all, yay!
Another use case for an escape are Unicode symbols. You can write any Unicode symbol using an escape sequence.
For example, an inverted question mark ¿ has the Unicode 00BF, so you can print it like this:
Additional Examples of “SyntaxError: Unexpected Character After Line Continuation Character” in Python
Here are more common examples of the unexpected character after line continuation character error:
Example #7
Often, you don’t have a specific file or folder path and have to assemble it from parts. You can do so via escape sequences \\ and string concatenations \. However, this manual piecing together regularly is the reason for the unexpected character after line continuation character error.
But the osmodule to the rescue! Use the path.join function. The path.join function not only does the path completion for you, but also determines the required separators in the path depending on the operating system on which you are running your program.
#os separator examples?
Example #8
You can get the line continuation error when you try to comment out # a line after a line continuation character \—you can’t do that in this way:
Remember from above that within parenthesis () an escape character is not needed? So put the string in parenthesis (), easy as that—and voila you can use comments # where ever you want.
You can put dummy parentheses, simply for hyphenation purposes:
Example #9
Another variation of the unexpected character after line continuation character error is when you try to run a script from the Python prompt. Here is a script correctly launched from the Windows command line:
However, if you type python first and hit enter, you will be taken to the Python prompt.
Here, you can directly run Python code such as print(“Hello, World!”). And if you try to run a file by analogy with the Windows command line, you will get an error:
Python SyntaxError: unexpected character after line continuation character Solution
![]()
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.
In this guide, we talk about what this error means and why it is raised. We walk through two examples of this error in action so you can learn how to use it in your code.
SyntaxError: unexpected character after line continuation character
The line continuation character lets you write a long string over multiple lines of code. This character is useful because it makes code easier to read. The line continuation character is a backslash (“\”).
Whereas it can be hard to follow a really long line of code, one line of code divided across multiple lines is easier to follow.
The line continuation character is commonly used to break up code or to write a long string across multiple lines of code:
We have broken up our string into three lines. This makes it easier to read our code.
Two scenarios when this error could be raised include:
- Using a backslash instead of a forward slash as a division operator
- Adding a new line to a string without enclosing the new line character in parenthesis
We’ll talk through each of these scenarios one-by-one.
Scenario #1: Division Using a Backslash
Here, we write a program that calculates a person’s body mass index (BMI). To start, we need to ask a user to insert their height and weight into a Python program:
Next, we calculate the user’s BMI. The formula for calculating a BMI value is:
“Kg” is a person’s weight in kilograms. “m 2 ” is the height of a person squared. Translated into Python, the formula to calculate a BMI looks like this:
We convert the values of “weight” and “height” to floating point numbers so that we can perform mathematical functions on them.
We then print a user’s BMI to the console. We convert “bmi” to a string using the str() method so that we can concatenate it to the “Your BMI is: ” message. We round the value of “bmi” to two decimal places using the round() method.
Let’s run our code:
We’ve encountered an error. This is because we have used “\” as the division operator instead of the “/” sign. We can fix our code by using the “/” division operator:
Our code returns:
Our code has successfully calculated the BMI of a user.
Scenario #2: Using the New Line Character Incorrectly
Next, we write a program that writes a list of ingredients to a file. We start by defining a list of ingredients for a shortbread recipe:
Next, we open up a file called “shortbread_recipe.txt” to which we will write our list of ingredients:
This code loops through every ingredient in the “ingredients” variable. Each ingredient is written to the ingredients file followed by a new line character in Python (“\n”). This makes sure that each ingredient appears on a new line.
Let’s run our Python code:
Our code returns an error. This is because we have not enclosed our new line character in quotation marks.
While the new line character is a special character, it must be enclosed within quotation marks whenever it is used. This is because Python treats “\” as a line continuation character.
To solve the error in our code, we need to enclose the newline character in double quotes:
Let’s run our code. Our code returns no value to the console. A new file called “shortbread_recipe.txt” is created. Its contents are as follows:
Our code has successfully printed our list to the “shortbread_recipe.txt” file.

«Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!»
Venus, Software Engineer at Rockbot
Find Your Bootcamp Match
Conclusion
The “SyntaxError: unexpected character after line continuation character” error is raised when you add code after a line continuation character.
To solve this error, make sure that you use the correct division operator (a forward slash) if you are performing mathematical operations. If you are using any special characters that contain a backslash, like the new line character, make sure that they are enclosed within quotation marks.
Now you’re ready to fix this error in your code!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.