简体   繁体   中英

Why Does Python print() function return EOL?

I am trying to use the Python print() function with a multi-line string literal -- I am using a vertical ellipsis to indicate multiple lines, not content, of the string literal.

print("Here is the data table: 
        .
        .
        .                   ")

Why am I getting EOL while scanning string literal error.

Why is this happening?

Use """ or ''' for multiline string literals .

Edit: as pointed out by @lvc in the comments, since you have parens around the string, you can still use the single quotes, you just have to start and end each line with " .

A string beginning with a single quote (either " or ') in python cannot span multiple lines. You must use a triple-quote string. For example:

print("""Here is the data table:
    ....
""")

Try using """ instead of " while delimiting the string:

print """
multiline string

...
"""

Three options:

  1. Use triple quotes as suggested by the rest. The annoying thing is that the first line of the string doesn't line up with the rest in the code.

  2. Use backslashes, pluses and multiple strings like this:

    print ("Here is the data table:"
    ""
    "...."
    ""
    )

  3. Use newline characters ( \\n ) in the string like this:

    print ("Here is the data table:\\n...\\n\\n\\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM