问题描述
我正在尝试学习 Python,但在进行练习时出现此错误.我的代码:
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.
推荐答案
正如前面所说,你制作了一个没有结尾的字符串.
老实说,我真的不明白您使用这个三重引号要做什么,所以我无法提供您想要的代码.但请记住:
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:
三重引号用于定义字符串.
您已经知道 "
表示字符串的开头或结尾."""
也是如此!所以如果你用"""
开始一个句子,你必须用"""
结束它.
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."""
这就是为什么您会收到错误消息.指令 print "Hi my name is %s" % 'Jonas'
在这里实际上并没有被解释为指令,它被解释为字符串的第二行,因为它位于标记 """
,表示一个字符串已经开始.这个字符串甚至没有完全定义,因为代码缺少标记字符串结束的"""
.
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.
这篇关于扫描三重引号字符串文字时的 Python EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!