【问题标题】:How to call various strings without using %s? [duplicate]如何在不使用 %s 的情况下调用各种字符串? [复制]
【发布时间】:2019-12-18 04:25:13
【问题描述】:
text1="Python"
text2="with me"
print("Study %(language)s" %{'language':text1})
  1. 这行得通。但我想知道它是否使用字典来调用字符串?

print("Study %(language)s %(with whom)" %({'language':text1},{'with whom':text2}))

  1. 但它不起作用。我该如何解决?

错误提示“格式需要映射”

【问题讨论】:

    标签: python


    【解决方案1】:

    如果你注意到这一点,它会起作用的:

    1. 您忘记在%(with whom) 之后添加s --> %(with whom)s
    2. 而不是这个%({'language':text1},{'with whom':text2}) --> %{'language':text1,'with whom':text2}

    所以这条线会是这样的:

    print("Study %(language)s %(with whom)s" %{'language':text1,'with whom':text2})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      相关资源
      最近更新 更多