Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python EOF while scanning triple-quoted string literal

Tags:

python-2.7

I am trying to learn Python and while doing an exercise this error showed. My code:

a = "Half of \nthis sentence."
b = "\tSo far away."
"""
print "Hi my name is %s" % 'Jonas'

It's the triple quotes but why? I was just trying to make a space empty under the line so I could have a gap between the two.

like image 760
Sneezy Avatar asked Jun 22 '16 07:06

Sneezy


1 Answers

As it was already said, you have made a string which has no end.

To be honest, I don't really understand what you are trying to do by using this triple quote, so I can't provide the code you want. But keep in mind that:

Triple quotes are used in order to define a string.

You already know that " means either the start or the end of a string. It is also the case for """ ! So if you begin a sentence with """, you have to close it with """.

print """Hello"""

These triple quotes are useful in some cases, when you have to print some characters like " or '.

print """I have found the letter 'H' in the word "Hello"."""

They also have a certain feature; they allow to define long strings on several lines.

print """The width of my
screen prevents me from
writing long sentences."""

And that is why you get an error. The instruction print "Hi my name is %s" % 'Jonas' is not actually interpretated as an instruction here, it is interpretated as the second line of a string, because it is located after the mark """, which means that a string has begun. And this string is not even completely defined because the code lacks of the """ which marks the end of the string.

like image 134
Lorèloi Avatar answered Sep 17 '22 13:09

Lorèloi