【问题标题】:Formatters in PythonPython中的格式化程序
【发布时间】:2019-08-10 09:05:12
【问题描述】:

我有这行代码:

formatter = "%r %r %r %r"

打印格式化程序 % (1, 2, 3, 4) 打印格式化程序 %(“一”、“二”、“三”、“四”) 打印格式化程序 % (True, False, False, True) 打印格式化程序 %(格式化程序、格式化程序、格式化程序、格式化程序) 打印格式化程序 % ( “我有这个东西给你。”, "你可以正确输入。", "但它没有唱歌。", “所以我说晚安。” )

最后 4 行的输出是:

“我有这个东西给你。” “你可以正确输入。” “但它没有唱歌。” “所以我说晚安。”

我想明白为什么第三句用双引号显示,而其他句子用单引号显示。

我已经尝试删除双引号,但它没有产生预期的结果。


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 for you.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."

【问题讨论】:

    标签: python-2.7 formatter


    【解决方案1】:

    这是因为字符串中有撇号'。如果删除它,它将打印为'But it didnt sing.' 这是为了区分字符串限制。如果用'打印会有些混乱:'But it didn't sing.'

    PS。如果您将'" 符号都添加到您的字符串中,那么事情会变得更加有趣。自己试试吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多