Python 3:使用 %s 和 .format()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14753844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:19:54  来源:igfitidea点击:

Python 3: using %s and .format()

pythonstringstring-formatting

提问by Zaur Nasibov

I have finally switched from %to the .format()string formatting operator in my 2.x code in order to make it easier to migrate to 3.x in future. It was a bit surprising to find out that not only the %-style formatting remains in Py3, but it is widely used in the standard library code. It seems logical, because writing '(%s)' % variableis a bit shorter and maybe easier to comprehend than '({})'.format(variable). But I'm still in doubt. Is it proper (pythonic?) to use both approaches in the code? Thank you.

我终于在我的 2.x 代码中从字符串格式化操作符切换%到了.format(),以便将来更容易迁移到 3.x。发现不仅%在 Py3 中保留了-style 格式,而且在标准库代码中广泛使用它,这有点令人惊讶。这似乎合乎逻辑,因为写作'(%s)' % variable'({})'.format(variable). 但我仍然怀疑。在代码中使用这两种方法是否合适(pythonic?)?谢谢你。

采纳答案by Vlad

Python 3.2 documentation said that, %will eventually go away.

Python 3.2 文档说,%最终会消失。

http://docs.python.org/3.2/tutorial/inputoutput.html#old-string-formatting

http://docs.python.org/3.2/tutorial/inputoutput.html#old-string-formatting

Since str.format()is quite new, a lot of Python code still uses the %operator. However, because this old style of formatting will eventually be removed from the language, str.format()should generally be used.

由于str.format()它很新,很多 Python 代码仍然使用%运算符。但是,因为这种旧的格式样式最终会从语言中删除,所以str.format()一般应该使用。

But as @regilero says, the sentence is gone from 3.3, which might suggest it's not actually the case. There are some conversations herethat suggest the same thing.

但正如@regilero 所说这句话从 3.3 中消失了,这可能表明情况并非如此。有一些谈话这里暗示了同样的事情。

As of Python 3.4 the paragraph 7.1.1reads:

Python 3.4 开始,第 7.1.1 段内容如下:

The % operator can also be used for string formatting. It interprets the left argument much like a sprintf()-style format string to be applied to the right argument, and returns the string resulting from this formatting operation.

% 运算符也可用于字符串格式化。它将左参数解释为很像要应用于右参数的 sprintf() 样式格式字符串,并返回此格式化操作产生的字符串。

See also Python 3.4 4.7.2 printf-style String Formatting.

另请参阅Python 3.4 4.7.2 printf 风格的字符串格式。

回答by Guy with 6 Magikarps.

"%s" is now "{}", so insted of adding %s replace it with {} where you would want to add the variable into the string.

“%s”现在是“{}”,因此添加 %s 将其替换为 {},您希望将变量添加到字符串中。

def main():
    n="Python 3.+" 
    l="looks nice"
    f="does not look practical."


    print("This seems to be the new way {}".format(n)\
      + "will be working, rather than the ' % ',{} but {}".format(l,f))



main()

#In comparison to just injecting the variable

Output disregard the quotes they are for illustration reasons

出于说明原因,输出忽略它们的引号

"This seems to be the new way "Python 3.+" will be working, rather than the '%;, "looks nice" but "does not look practical.""

“这似乎是“Python 3.+”的新工作方式,而不是 '%;,“看起来不错”但“看起来不实用”。