Python syntaxerror: eol while scanning string literal
Tags: python, programming, codenewbie
Published: 2021-09-04 09:20:39

---
title: Python syntaxerror: eol while scanning string literal
published: true
date: 2021-09-04 06:24:18 UTC
tags: Python,#programming,#codenewbie
canonical_url: https://itsmycode.com/python-syntaxerror-eol-while-scanning-string-literal/
---

ItsMyCode |

Python is an interpreted language that means, unlike other languages( **Java, C++, C#, etc.** ), Python doesn’t convert the entire code into low-level machine code at once; rather, each line of code is executed line by line.

The Syntax Error mainly occurs when the Python interpreter finds some anomalies in your code. Some of the common issues are “ **missing out the brackets** ,” **“missing quotes** ,” “ **if-else mismatch** ,” etc.   

## What does EOL while scanning string literal error mean in Python?

**EOL** stands for “ **End of line** ,” and the Syntax error means that we have not followed the guidelines of Python Programming. 

If you read the error carefully, Python is expecting a character at the end of the line. In this instance, Python expects the string closing character ( **`"`** ), which needs to be closed at the end of the line.

The  **eol while scanning string literal error**  in Python occurs in 3 main use cases.

1. **Missing quotation marks**
2. **String spanning multiple lines using the wrong syntax**
3. **Mismatch of quotes**
4. **Using backslash before the ending quotation mark**

## Missing quotation marks

String in Python must be opened and closed properly with quotations marks; else, you will get a syntax error. Let’s take an example of a string that is not closed.

```Python
def getName():
    print("My name is Chandler Bing)
getName()

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 2
    return "My name is Chandler Bing
                                    ^
SyntaxError: EOL while scanning string literal
```

**Solution –**  We can easily fix this error by closing the string with the same quotation mark we used to open our string in the beginning.

```Python
def getName():
    print("My name is Chandler Bing")
getName()

# Output
My name is Chandler Bing
```

## String spanning multiple lines using the wrong syntax

In some cases, the string value can span multiple lines if you don’t use a proper syntax in the multi-line string, then Python will throw  **eol while scanning string literal error.**

```Python
def getMessage():
    message= "This is Chandler Bing and i am one of the famous actor in the 
    TV Series called Friends. Welcome to My world"
    print(message)

getMessage()

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 2
    message= "This is Chandler Bing and i am one of the famous actor in the 
                                                                            ^
SyntaxError: EOL while scanning string literal
```

**Solution – ** If you have a multi-line string in Python, the best way to declare it is by enclosing it using triple quotes. 

Either enclose it by using 3 single quotation**`(''' Hello ''')`** marks or 3 double quotation **`(""" Hello """)`** marks to resolve the issue. 

Alternatively, you can declare the string in one line and use **`\n`** to split wherever required into multi-line.

```Python
def getMessage():
    message= """This is Chandler Bing and i am one of the famous actor in the 
    TV Series called Friends. Welcome to My world"""
    print(message)

getMessage()

# Output
This is Chandler Bing and i am one of the famous actor in the 
    TV Series called Friends. Welcome to My world
```

## Mismatch of quotes

The type of quote used to open a string should be the same as closing the string that means if you have used single quotes **(`'`)** to open a string, do use single quotes **(`'`)** to close a string. 

```Python
def getMessage():
    message= "Hello World'
    print(message)

getMessage()

# Output
 File "c:\Projects\Tryouts\listindexerror.py", line 2
    message= "Hello World'
                          ^
SyntaxError: EOL while scanning string literal
```

**Solution –**  Changing the quotation mark**(`"`)** to match the beginning of the string will resolve the issue here.

```Python
def getMessage():
    message= "Hello World"
    print(message)
getMessage()

# Output
Hello World

```

## Using backslash before the ending quotation mark

In storing the directory path in a variable, we often use backlash, which will lead to  **eol while scanning string literal error.**

It causes a Syntax error because Python interprets backslash**`(\)`** as an escape sequence. So, in this case, it will treat both **`(\") `**as a single character, and according to Python, the string is not enclosed properly.

```Python
# Storing a directory path 
folderPath= "C:\Program Files\Python\"
print(folderPath)

# Output
  File "c:\Projects\Tryouts\listindexerror.py", line 2
    folderPath= "C:\Program Files\Python\"
                                          ^
SyntaxError: EOL while scanning string literal
```

**Solution- ** Replace the backslash with an escape sequence, adding double backslash so that Python can interpret and execute properly.

```Python
# Storing a directory path 
folderPath= "C:\\Program Files\\Python\\"
print(folderPath)

# Output
C:\Program Files\Python\
```

The post [Python syntaxerror: eol while scanning string literal](https://itsmycode.com/python-syntaxerror-eol-while-scanning-string-literal/) appeared first on [ItsMyCode](https://itsmycode.com).