[英]double and single quotes in python
hello I am currently learning python and I've been stuck in this small script thats written in my ebook. 你好,我目前正在学习python,我一直被困在我的电子书中写的这个小脚本中。
formatter = "%r %r %r %r"
print formatter %(1, 2, 3, 4)
print formatter %("one", "two", "three", "four")
print formatter %(True, False, False, True)
print formatter %(formatter, formatter, formatter, formatter)
print formatter %(
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
When I run the code every other string is in single quotes but the "But it didn't sing" is in double quotes. 当我运行代码时,其他所有字符串都用单引号引起来,但是“但是没有唱歌”用双引号引起来。 After trying several syntax i understood that the single quote in didn't has something to do with this.
在尝试了几种语法后,我了解到单引号与此无关。 But why does this happen?
但是为什么会这样呢? If I assign that line to a variable and print it like
如果我将该行分配给变量并像打印那样
yathartha = "But it didn't happen"
print "%r" %yathartha
the output is "But it didn't happen" with the double quotes. 输出为带有双引号的“但是没有发生”。 Isn't there supposed to be single quote?
难道不应该有单引号吗?
Well, when you are talking about a string, it is present between the quotes, so if your string contains a single quote, it can't use single quotes to enclose it because, if it starts with a single quote, it'll end at the first single quote. 好吧,当您谈论一个字符串时,它出现在引号之间,因此,如果您的字符串包含一个单引号,则不能使用单引号将其括起来,因为如果它以一个单引号开头,它将结束用第一个单引号引起来。
yathartha = "But it didn't happen"
string_var = "%r" %yathartha
print string_var
If your string was enclosed in single quotes then your string which would get stored in string_var would be 'But it didn' because that would be the closing quote that it encounters. 如果您的字符串用单引号引起来,那么将存储在string_var中的字符串将为“ But it not”,因为那是遇到的结尾引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.