字符串格式:% vs. .format vs. f-string文字 [英] String formatting: % vs. .format vs. f-string literal

查看:22
本文介绍了字符串格式:% vs. .format vs. f-string文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 2.6 引入了 str.format() 方法的语法与现有的 % 运算符略有不同.哪个更好,适用于哪些情况?

Python 2.6 introduced the str.format() method with a slightly different syntax from the existing % operator. Which is better and for what situations?

Python 3.6 现在通过语法 fmy string" 引入了另一种字符串格式化格式的字符串文字(又名f"字符串).这个格式化选项比其他的更好吗?

Python 3.6 has now introduced another string formatting format of string literals (aka "f" strings) via the syntax f"my string". Is this formatting option better than the others?

  1. 以下使用每种方法,结果都一样,那么有什么区别?

  1. The following uses each method and has the same outcome, so what is the difference?

 #!/usr/bin/python
 sub1 = "python string!"
 sub2 = "an arg"

 sub_a = "i am a %s" % sub1
 sub_b = "i am a {0}".format(sub1)
 sub_c = f"i am a {sub1}"

 arg_a = "with %(kwarg)s!" % {'kwarg':sub2}
 arg_b = "with {kwarg}!".format(kwarg=sub2)
 arg_c = f"with {sub2}!"

 print(sub_a)    # "i am a python string!"
 print(sub_b)    # "i am a python string!"
 print(sub_c)    # "i am a python string!"

 print(arg_a)    # "with an arg!"
 print(arg_b)    # "with an arg!"
 print(arg_c)    # "with an arg!"

  • 此外,Python 中何时进行字符串格式化?例如,如果我的日志记录级别设置为 HIGH,我是否仍会因执行以下 % 操作而受到打击?如果是这样,有没有办法避免这种情况?

  • Furthermore when does string formatting occur in Python? For example, if my logging level is set to HIGH will I still take a hit for performing the following % operation? And if so, is there a way to avoid this?

     log.debug("some debug info: %s" % some_info)
    

  • 推荐答案

    回答你的第一个问题... .format 在很多方面似乎更复杂.% 的一个令人讨厌的事情是它如何接受一个变量或一个元组.您会认为以下方法总是有效的:

    To answer your first question... .format just seems more sophisticated in many ways. An annoying thing about % is also how it can either take a variable or a tuple. You'd think the following would always work:

    "hi there %s" % name
    

    然而,如果 name 恰好是 (1, 2, 3),它会抛出一个 TypeError.为了保证它总是打印,你需要做

    yet, if name happens to be (1, 2, 3), it will throw a TypeError. To guarantee that it always prints, you'd need to do

    "hi there %s" % (name,)   # supply the single argument as a single-item tuple
    

    这只是丑陋..format 没有这些问题.同样在您提供的第二个示例中,.format 示例看起来更清晰.

    which is just ugly. .format doesn't have those issues. Also in the second example you gave, the .format example is much cleaner looking.

    你为什么不使用它?

    • 不知道(在阅读本文之前是我)
    • 必须与 Python 2.5 兼容

    为了回答您的第二个问题,字符串格式化与任何其他操作同时发生 - 评估字符串格式化表达式时.并且 Python 不是一种惰性语言,它在调用函数之前评估表达式,因此在您的 log.debug 示例中,表达式 "some debug info: %s"%some_info 将首先评估,例如一些调试信息:roflcopters 处于活动状态",然后该字符串将被传递给 log.debug().

    To answer your second question, string formatting happens at the same time as any other operation - when the string formatting expression is evaluated. And Python, not being a lazy language, evaluates expressions before calling functions, so in your log.debug example, the expression "some debug info: %s"%some_infowill first evaluate to, e.g. "some debug info: roflcopters are active", then that string will be passed to log.debug().

    这篇关于字符串格式:% vs. .format vs. f-string文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆