The Python line continuation character lets you continue a line of code on a new line in your program. The line continuation character cannot be followed by any value.
If you specify a character or statement after a line continuation character, you encounter the “SyntaxError: unexpected character after line continuation character” error.
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:
url = "https://careerkarma.com" \ "/blog/python-syntaxerror-unexpected-character-after" \ "line-continuation-character"
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:
height = input("What is your height? ") weight = input("What is your weight? ")
Next, we calculate the user’s BMI. The formula for calculating a BMI value is:
BMI = kg/m2
“Kg” is a person’s weight in kilograms. “m2” is the height of a person squared. Translated into Python, the formula to calculate a BMI looks like this:
bmi = float(weight) \ (float(height) * 2) print("Your BMI is: " + str(bmi))
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:
File "main.py", line 4 bmi = float(weight) \ (float(height) * 2) ^ SyntaxError: unexpected character after line continuation character
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:
bmi = float(weight) / (float(height) * 2) print("Your BMI is: " + str(round(bmi, 2)))
Our code returns:
What is your height? 1.70 What is your weight? 63 Your BMI is: 18.53
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:
ingredients = [ "150g plain flour", "100g butter, chilled an cubed", "50g caster sugar" ]
Next, we open up a file called “shortbread_recipe.txt” to which we will write our list of ingredients:
with open("shortbread_recipe.txt", "w+") as ingredients_file: for i in ingredients: ingredients_file.write(i + \n)
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:
File "main.py", line 9 ingredients_file.write(i + \n) ^ SyntaxError: unexpected character after line continuation character
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:
with open("shortbread_recipe.txt", "w+") as ingredients_file: for i in ingredients: ingredients_file.write(i + "\n")
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:
150g plain flour 100g butter, chilled an cubed 50g caster sugar
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
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.