字符串格式:% 与 .format 与 f 字符串文字
字符串格式化方法有多种:
- Python <2.6:
"Hello %s" % name
- Python 2.6+:
"Hello {}".format(name)
(使用str.format
) - Python 3.6+:
f"{name}"
(使用 f 字符串)
哪个更好,适用于什么情况?
以下方法的结果相同,那么有什么区别呢?
<前><代码>名称=“爱丽丝” “你好 %s” % 姓名 “你好{0}”.format(名称) f“你好{名字}” # 使用命名参数: “你好 %(kwarg)s” % {'kwarg': 名称} “你好{kwarg}”.format(kwarg=名称) f“你好{名字}”何时运行字符串格式化,以及如何避免运行时性能损失?
如果您尝试关闭只是寻找格式化字符串的方法的重复问题,请使用如何放置变量的字符串内的值?.
There are various string formatting methods:
- Python <2.6:
"Hello %s" % name
- Python 2.6+:
"Hello {}".format(name)
(usesstr.format
) - Python 3.6+:
f"{name}"
(uses f-strings)
Which is better, and for what situations?
The following methods have the same outcome, so what is the difference?
name = "Alice" "Hello %s" % name "Hello {0}".format(name) f"Hello {name}" # Using named arguments: "Hello %(kwarg)s" % {'kwarg': name} "Hello {kwarg}".format(kwarg=name) f"Hello {name}"
When does string formatting run, and how do I avoid a runtime performance penalty?
If you are trying to close a duplicate question that is just looking for a way to format a string, please use How do I put a variable’s value inside a string?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
要回答你的第一个问题...
.format
在很多方面似乎更复杂。%
的一个令人烦恼的事情是它如何接受变量或元组。您可能认为以下内容总是有效:但是,如果
name
恰好是(1, 2, 3)
,它将抛出TypeError
>。为了保证它总是打印出来,你需要做一些很丑陋的事情。
.format
不存在这些问题。同样在您给出的第二个示例中,.format
示例看起来更加简洁。仅将其用于向后兼容 Python 2.5。
要回答第二个问题,字符串格式化与任何其他操作同时发生 - 当评估字符串格式化表达式时。 Python 不是一种惰性语言,它会在调用函数之前计算表达式,因此表达式
log.debug("some debug info: %s" % some_info)
首先会计算字符串,例如“一些调试信息:roflcopters 处于活动状态”
,然后该字符串将传递给log.debug()
。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:yet, if
name
happens to be(1, 2, 3)
, it will throw aTypeError
. To guarantee that it always prints, you'd need to dowhich is just ugly.
.format
doesn't have those issues. Also in the second example you gave, the.format
example is much cleaner looking.Only use it for backwards compatibility with Python 2.5.
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 the expression
log.debug("some debug info: %s" % some_info)
will first evaluate the string to, e.g."some debug info: roflcopters are active"
, then that string will be passed tolog.debug()
.模运算符( % )无法做到的事情,据我所知:
结果
非常有用。
另一点:
format()
作为一个函数,可以用作其他函数中的参数:结果:
Something that the modulo operator ( % ) can't do, afaik:
result
Very useful.
Another point:
format()
, being a function, can be used as an argument in other functions:Results in:
假设您使用 Python 的
logging
模块,您可以将字符串格式化参数作为参数传递给.debug()
方法,而不是自己进行格式化:这可以避免执行除非记录器实际记录了某些内容,否则进行格式化。
Assuming you're using Python's
logging
module, you can pass the string formatting arguments as arguments to the.debug()
method rather than doing the formatting yourself:which avoids doing the formatting unless the logger actually logs something.
从 Python 3.6 (2016) 开始,您可以使用 f-strings< /em> 替换变量:
请注意
f"
前缀。如果您在 Python 3.5 或更早版本中尝试此操作,您将收到SyntaxError
。请参阅 < a href="https://docs.python.org/3.6/reference/lexical_analysis.html#f-strings" rel="noreferrer">https://docs.python.org/3.6/reference/lexical_analysis.html# f 字符串
As of Python 3.6 (2016) you can use f-strings to substitute variables:
Note the
f"
prefix. If you try this in Python 3.5 or earlier, you'll get aSyntaxError
.See https://docs.python.org/3.6/reference/lexical_analysis.html#f-strings
PEP 3101 建议替换
%
运算符Python 3 中采用了新的高级字符串格式,这将是默认设置。PEP 3101 proposes the replacement of the
%
operator with the new, advanced string formatting in Python 3, where it would be the default.不过请注意,刚才我在尝试将现有代码中所有
%
替换为.format
时发现了一个问题:'{}' .format(unicode_string)
将尝试对 unicode_string 进行编码,并且可能会失败。只要看看这个 Python 交互式会话日志:
s
只是一个字符串(称为“字节数组”)在 Python3 中)并且u
是一个 Unicode 字符串(在 Python3 中称为“字符串”):当您将 Unicode 对象作为参数提供给
%
运算符时,它将生成一个 Unicode 字符串即使原始字符串不是 Unicode:但
.format
函数将引发“UnicodeEncodeError”:并且仅当原始字符串是 Unicode 时,它才可以正常使用 Unicode 参数。
或者如果参数字符串可以转换为字符串(所谓的“字节数组”)
But please be careful, just now I've discovered one issue when trying to replace all
%
with.format
in existing code:'{}'.format(unicode_string)
will try to encode unicode_string and will probably fail.Just look at this Python interactive session log:
s
is just a string (called 'byte array' in Python3) andu
is a Unicode string (called 'string' in Python3):When you give a Unicode object as a parameter to
%
operator it will produce a Unicode string even if the original string wasn't Unicode:but the
.format
function will raise "UnicodeEncodeError":and it will work with a Unicode argument fine only if the original string was Unicode.
or if argument string can be converted to a string (so called 'byte array')
根据我的测试,
%
的性能比format
更好。测试代码:
Python 2.7.2:
结果:
Python 3.5.2
结果
在Python2中,差异很小,而在Python3中,
%
比format
快得多。感谢@Chris Cogdon 提供的示例代码。
编辑1:
2019年7月在Python 3.7.2中再次测试。
结果:
没有太大区别。我猜Python正在逐渐改进。
编辑2:
有人在评论中提到python 3的f-string后,我在python 3.7.2下对以下代码进行了测试:
结果:
看来f-string仍然比
%
慢,但比格式
。%
gives better performance thanformat
from my test.Test code:
Python 2.7.2:
Result:
Python 3.5.2
Result
It looks in Python2, the difference is small whereas in Python3,
%
is much faster thanformat
.Thanks @Chris Cogdon for the sample code.
Edit 1:
Tested again in Python 3.7.2 in July 2019.
Result:
There is not much difference. I guess Python is improving gradually.
Edit 2:
After someone mentioned python 3's f-string in comment, I did a test for the following code under python 3.7.2 :
Result:
It seems f-string is still slower than
%
but better thanformat
..format
的另一个优点(我在答案中没有看到):它可以采用对象属性。或者,作为关键字参数:
据我所知,这是不可能的
%
。Yet another advantage of
.format
(which I don't see in the answers): it can take object properties.Or, as a keyword argument:
This is not possible with
%
as far as I can tell.正如我今天发现的,通过
%
格式化字符串的旧方法不支持开箱即用的Decimal
(Python 的十进制定点和浮点算术模块)。示例(使用 Python 3.3.5):
输出:
当然可能有解决方法,但您仍然可以考虑立即使用
format()
方法。As I discovered today, the old way of formatting strings via
%
doesn't supportDecimal
, Python's module for decimal fixed point and floating point arithmetic, out of the box.Example (using Python 3.3.5):
Output:
There surely might be work-arounds but you still might consider using the
format()
method right away.如果你的 python >= 3.6,F 字符串格式的文字就是你的新朋友。
它更简单、更干净、性能也更好。
If your python >= 3.6, F-string formatted literal is your new friend.
It's more simple, clean, and better performance.
附带说明一下,使用带有日志记录的新样式格式并不一定会影响性能。您可以将任何对象传递给实现
__str__
魔术方法的logging.debug
、logging.info
等。当日志记录模块决定必须发出消息对象(无论它是什么)时,它会在执行此操作之前调用str(message_object)
。所以你可以这样做:这在Python 3文档中都有描述(https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles)。但是,它也适用于 Python 2.6 ( https://docs.python.org/2.6/library/logging.html#using-centric-objects-as-messages)。
使用此技术的优点之一,除了它与格式样式无关之外,还在于它允许使用惰性值,例如上面的函数
expense_func
。这为 Python 文档中给出的建议提供了更优雅的替代方案:https: //docs.python.org/2.6/library/logging.html#optimization。As a side note, you don't have to take a performance hit to use new style formatting with logging. You can pass any object to
logging.debug
,logging.info
, etc. that implements the__str__
magic method. When the logging module has decided that it must emit your message object (whatever it is), it callsstr(message_object)
before doing so. So you could do something like this:This is all described in the Python 3 documentation (https://docs.python.org/3/howto/logging-cookbook.html#formatting-styles). However, it will work with Python 2.6 as well (https://docs.python.org/2.6/library/logging.html#using-arbitrary-objects-as-messages).
One of the advantages of using this technique, other than the fact that it's formatting-style agnostic, is that it allows for lazy values e.g. the function
expensive_func
above. This provides a more elegant alternative to the advice being given in the Python docs here: https://docs.python.org/2.6/library/logging.html#optimization.%
可能会有所帮助的一种情况是当您格式化正则表达式时。例如,引发
IndexError
。在这种情况下,您可以使用:这可以避免将正则表达式编写为
'{type_names} [az]{{2}}'
。当您有两个正则表达式时,这会很有用,其中一个正则表达式单独使用而不带格式,但两个正则表达式的串联都已格式化。One situation where
%
may help is when you are formatting regex expressions. For example,raises
IndexError
. In this situation, you can use:This avoids writing the regex as
'{type_names} [a-z]{{2}}'
. This can be useful when you have two regexes, where one is used alone without format, but the concatenation of both is formatted.我想补充一点,从 3.6 版本开始,我们可以使用 fstrings,如下
所示
所有内容都转换为字符串
结果:
你可以传递函数,就像其他格式的方法
例如
I would add that since version 3.6, we can use fstrings like the following
Which give
Everything is converted to strings
Result:
you can pass function, like in others formats method
Giving for example
Python 3.6.7 比较:
输出:
Python 3.6.7 comparative:
Output:
对于 python 版本 >= 3.6(请参阅 PEP 498)
For python version >= 3.6 (see PEP 498)
但一件事是,如果您有嵌套大括号,则不适用于格式,但
%
可以。例子:
But one thing is that also if you have nested curly-braces, won't work for format but
%
will work.Example: