SyntaxError: EOL while scanning string literal in Python

Dung Do Tien Aug 19 2021 51

Hello Guys, I am a newbie in Python, and I have passages that need to print out the console window as below:

 myMessage = "Another change is that the touchscreen looks more seamlessly integrated into the dash. 
     It no longer has the physical buttons on its sides, and instead it now has touch buttons. 
     Also, the said infotainment screen is now an eight-inch unit that comes with Apple CarPlay and Android Auto." 
 
# Printing the message 
print(myMessage)

I want to print it out in multiple lines. But when I run, I got an error SyntaxError: EOL while scanning string literal

 SyntaxError: EOL while scanning string literal

  File "main.py", line 1
    myMessage = "Another change is that the touchscreen looks more seamlessly integrated into the dash. 
                                                                                                       ^
SyntaxError: EOL while scanning string literal

My environment:

 OS: Window 10
Tool: VS Code
Python: version 3.8.2

Anybody can explain to me what's it mean?

Thanks for any response!!

Have 2 answer(s) found.
  • S

    Suthep Sangvirotjanaphat Aug 19 2021

    EOL while scanning string literal error throw when string data type does not end with a " or '

    If you want to break a new line and print out it into the console you can use \n to help create a new line. 

     myMessage = "This is line 1 \n This is line 2 \n This is line 3" 
     
    # Printing the message 
    print(myMessage)
     # output below
    This is line 1 
     This is line 2 
     This is line 3

    hope it's useful for you!

  • T

    Teravut Anansiripinyo Aug 19 2021

    Strings can't normally span multiple lines. If you don't want the string to appear on multiple lines but you want to initialize it on multiple lines (so you can read it more easily), you can "escape" the newline by putting a backslash before the newline. If you want it to appear on multiple lines,  you can use triple quotes around the string.

     foo = """Line 1
          Line 2
          Line 3"""
    print(foo)
     #Output
    Line 1
          Line 2
          Line 3 
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close