This question already has an answer here:
这个问题在这里已有答案:
- Python string formatting: % vs. .format 14 answers
- Python字符串格式:%vs. .format 14个答案
In Python there seem to be two different ways of generating formatted output:
在Python中,似乎有两种不同的生成格式化输出的方法:
user = "Alex"
number = 38746
print("%s asked %d questions on *.com" % (user, number))
print("{0} asked {1} questions on *.com".format(user, number))
Is there one way to be preferred over the other? Are they equivalent, what is the difference? What form should be used, especially for Python3?
有没有一种方法可以优先于另一种方式?它们是等价的,有什么区别?应该使用什么形式,特别是对于Python3?
4 个解决方案
#1
91
Use the format
method, especially if you're concerned about Python 3 and the future. From the documentation:
使用format方法,特别是如果您关注Python 3和未来。从文档:
The formatting operations described here [% substitutions] are obsolete and may go away in future versions of Python. Use the new String Formatting in new code.
这里描述的格式化操作[%substitutions]已经过时,可能会在未来的Python版本中消失。在新代码中使用新的字符串格式。
#2
18
.format
was introduced in Python2.6
.format是在Python2.6中引入的
If you need backward compatibility with earlier Python, you should use %
如果你需要向后兼容早期的Python,你应该使用%
For Python3 and newer you should use .format
for sure
对于Python3和更新版本,您应该使用.format
.format
is more powerful than %
. Porting %
to .format
is easy but the other way round can be non trivial
.format比%更强大。将%移植到.format很容易,但反过来可能非常简单
#3
7
You can use both .No one said % formatting expression is deprecated.However,as stated before the format method call is a tad more powerful. Also note that the % expressions are bit more concise and easier to code.Try them and see what suits you best
你可以使用两者。没有人说过%格式表达式已被弃用。但是,如前所述,格式方法调用有点强大。另请注意,%表达式更简洁,更容易编码。尝试使用它们,看看哪种最适合您
#4
6
The docs say that the format
method is preferred for new code. There are currently no plans to remove % formatting, though.
文档说格式方法是新代码的首选。但是,目前没有计划删除%格式。
#1
91
Use the format
method, especially if you're concerned about Python 3 and the future. From the documentation:
使用format方法,特别是如果您关注Python 3和未来。从文档:
The formatting operations described here [% substitutions] are obsolete and may go away in future versions of Python. Use the new String Formatting in new code.
这里描述的格式化操作[%substitutions]已经过时,可能会在未来的Python版本中消失。在新代码中使用新的字符串格式。
#2
18
.format
was introduced in Python2.6
.format是在Python2.6中引入的
If you need backward compatibility with earlier Python, you should use %
如果你需要向后兼容早期的Python,你应该使用%
For Python3 and newer you should use .format
for sure
对于Python3和更新版本,您应该使用.format
.format
is more powerful than %
. Porting %
to .format
is easy but the other way round can be non trivial
.format比%更强大。将%移植到.format很容易,但反过来可能非常简单
#3
7
You can use both .No one said % formatting expression is deprecated.However,as stated before the format method call is a tad more powerful. Also note that the % expressions are bit more concise and easier to code.Try them and see what suits you best
你可以使用两者。没有人说过%格式表达式已被弃用。但是,如前所述,格式方法调用有点强大。另请注意,%表达式更简洁,更容易编码。尝试使用它们,看看哪种最适合您
#4
6
The docs say that the format
method is preferred for new code. There are currently no plans to remove % formatting, though.
文档说格式方法是新代码的首选。但是,目前没有计划删除%格式。