SyntaxError: unexpected character after line continuation character
Tags: python, programming, codenewbie
Published: 2021-09-06 11:21:57

---
title: SyntaxError: unexpected character after line continuation character
published: true
date: 2021-09-06 03:23:19 UTC
tags: #python #programming #codenewbie
canonical_url: https://itsmycode.com/syntaxerror-unexpected-character-after-line-continuation-character/
---

ItsMyCode |

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

```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](https://itsmycode.com/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.

```python 
# 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.

```python 
# 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.

```python 
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.

```python 
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.

```python 
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")
```

The post [SyntaxError: unexpected character after line continuation character](https://itsmycode.com/syntaxerror-unexpected-character-after-line-continuation-character/) appeared first on [ItsMyCode](https://itsmycode.com).