Quote tricks in ex9

In ex9, three quotes can give you free space to type whatever you want. So I want to test the border of quotes used in this case.
Here’s the code.

 print("""
There's something going on here.
With the three double-quotes.
Wi'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
""")

The result is like this.

There's something going on here.
With the three double-quotes.
Wi'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.

What if we use 4 double-quotes instead of three? My expectation is like this:

"
There's something going on here.
With the three double-quotes.
Wi'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"

However, it shows error in this case:

  File "ex9.py", line 14
    """")
        ^
SyntaxError: EOL while scanning string literal

Then I use single-quotes to replace double-quotes.

print("""'
There's something going on here.
With the three double-quotes.
Wi'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
'""")

This time it successfully prints:

'
There's something going on here.
With the three double-quotes.
Wi'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
'

Then I tried four double-quotes again, but this time I place the fourth double-quote in a new line:

print("""
"
There's something going on here.
With the three double-quotes.
Wi'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"
""")

This time it shows as I expected in the first experiment:

"
There's something going on here.
With the three double-quotes.
Wi'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"

Quotes are interesting. Thank you for reading and feel free to discuss it with me! Other observations are also welcomed!

1 Like

So the #1 rule of programming languages is that the parser is always right, even if you think it’s wrong. If Python says this is wrong, then it’s wrong. I think that one mindset is something that bothers many beginners because they’re used to things where you can actually be a little wrong or right and work in a gray area. With computing it’s just wrong, or right. No gray area. And, many times it’s right only because someone else made it work that way and you just have to accept it (and they’ll swear up and down that they’re totally right even though everyone else doesn’t do it that way).

In this case, nope, if you do four " chars it’s just wrong. Have to put a space in there. And the explanation for why is, they just made it that way. Oh well.

1 Like