请教一个《Learn Python the Hard Way》上的问题。

雪团老四 2017-12-06 09:28:57
原代码如下图所示:

输出如下图所示:

请问一下,为什么程序的最后一行输出中,既有单引号,又有双引号?是什么导致的呢?
...全文
68 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪团老四 2017-12-06
  • 打赏
  • 举报
回复
谢谢,明白了
引用 2 楼 qq646748739 的回复:
第三行输出单引号和双引号,是因为didn't的原因。

>>> formatter = "%r %r %r %r"
>>> print (formatter % ("I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
))
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'
将didn't换成did not,就不会输出双引号了。

>>> print (formatter % ("I had this thing.",
"That you could type up right.",
"But it did not sing.",
"So I said goodnight."
))

'I had this thing.' 'That you could type up right.' 'But it did not sing.' 'So I said goodnight.'
雪团老四 2017-12-06
  • 打赏
  • 举报
回复
谢谢,讲得很清楚全面,明白了。
引用 1 楼 extend 的回复:
看第三句,里面有个单引号。 简单来说,看例子: 句子里面有单引号,要么用双号,要么转义,直接用单引号,错。 >>> print("let's go") let's go >>> print('let\'s go') let's go >>> print('let's go') SyntaxError: invalid syntax 句子里面有双引号,要么用单引号,要么转义,直接用双引号,错。 >>> print('let "go"') let "go" >>> print("let \"go\"") let "go" >>> print("let "go"") SyntaxError: invalid syntax 又有单引号,又有双引号怎么办。用单引双引无所谓,转义。 >>> print('let\'s "go"') let's "go" >>> print("let\'s \"go\"") let's "go" 所以你的第三句,里面有个单引号,输出结果一定是用双引号包围的。
碧水幽幽泉 2017-12-06
  • 打赏
  • 举报
回复
第三行输出单引号和双引号,是因为didn't的原因。


>>> formatter = "%r %r %r %r"
>>> print (formatter % ("I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
))
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'


将didn't换成did not,就不会输出双引号了。

>>> print (formatter % ("I had this thing.",
"That you could type up right.",
"But it did not sing.",
"So I said goodnight."
))

'I had this thing.' 'That you could type up right.' 'But it did not sing.' 'So I said goodnight.'
extend 2017-12-06
  • 打赏
  • 举报
回复
看第三句,里面有个单引号。 简单来说,看例子: 句子里面有单引号,要么用双号,要么转义,直接用单引号,错。 >>> print("let's go") let's go >>> print('let\'s go') let's go >>> print('let's go') SyntaxError: invalid syntax 句子里面有双引号,要么用单引号,要么转义,直接用双引号,错。 >>> print('let "go"') let "go" >>> print("let \"go\"") let "go" >>> print("let "go"") SyntaxError: invalid syntax 又有单引号,又有双引号怎么办。用单引双引无所谓,转义。 >>> print('let\'s "go"') let's "go" >>> print("let\'s \"go\"") let's "go" 所以你的第三句,里面有个单引号,输出结果一定是用双引号包围的。

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧