python - 如何将可变数量的参数格式化为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18391059/
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-19 10:37:44  来源:igfitidea点击:

python - How to format variable number of arguments into a string?

pythonstringstring-formatting

提问by Gerard

We know that formatting oneargument can be done using one%sin a string:

我们知道,格式化一个参数是可以做到用一个%s在一个字符串:

>>> "Hello %s" % "world"
'Hello world'

for two arguments, we can use two %s(duh!):

对于两个参数,我们可以使用两个%s(废话!):

>>> "Hello %s, %s" % ("John", "Joe")
'Hello John, Joe'

So, how can I format a variable number of arguments without having to explicitly define within the base string a number of %sequal to the number of arguments to format? it would be very cool if something like this exists:

那么,如何格式化可变数量的参数,而不必在基本字符串中显式定义%s与 format 的参数数量相等的数量?如果存在这样的东西,那就太酷了:

>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary")
Hello JohnJoeMary
>>> "Hello <cool_operator_here>" % ("John", "Joe", "Mary", "Rick", "Sophie")
Hello JohnJoeMaryRickSophie

Is this even possible or the only thing I could do about it is to do something like:

这是否可能,或者我唯一能做的就是做这样的事情:

>>> my_args = ["John", "Joe", "Mary"]
>>> my_str = "Hello " + ("".join(["%s"] * len(my_args)))
>>> my_str % tuple(my_args)
"Hello JohnJoeMary"

NOTE: I needto do it with the %sstring formatting operator.

注意:我需要使用%s字符串格式化操作符来完成。

UPDATE:

更新

It needs to be with the %sbecause a function from another library formats my string using that operator given that I pass the unformatted string and the args to format it, but it makes some checking and corrections (if needed) on the args before actually making the formatting.

它需要与 ,%s因为来自另一个库的函数使用该运算符格式化我的字符串,因为我传递了未格式化的字符串和 args 来格式化它,但它在实际制作之前对 args 进行了一些检查和更正(如果需要)格式化。

So I need to call it:

所以我需要调用它:

>>> function_in_library("Hello <cool_operator_here>", ["John", "Joe", "Mary"])
"Hello JohnJoeMary"

Thanks for your help!

谢谢你的帮助!

采纳答案by Martijn Pieters

You'd use str.join()on the list withoutstring formatting, then interpolate the result:

您可以str.join()没有字符串格式的列表上使用,然后插入结果

"Hello %s" % ', '.join(my_args)

Demo:

演示:

>>> my_args = ["foo", "bar", "baz"]
>>> "Hello %s" % ', '.join(my_args)
'Hello foo, bar, baz'

If some of your arguments are not yet strings, use a list comprehension:

如果您的某些参数还不是字符串,请使用列表推导式:

>>> my_args = ["foo", "bar", 42]
>>> "Hello %s" % ', '.join([str(e) for e in my_args])
'Hello foo, bar, 42'

or use map(str, ...):

或使用map(str, ...)

>>> "Hello %s" % ', '.join(map(str, my_args))
'Hello foo, bar, 42'

You'd do the same with your function:

你会对你的函数做同样的事情:

function_in_library("Hello %s", ', '.join(my_args))

If you are limited by a (rather arbitrary) restriction that you cannot use a joinin the interpolation argument list, use a jointo create the formatting string instead:

如果您受到不能join在插值参数列表中使用 a的(相当任意的)限制,请改用 ajoin创建格式化字符串:

function_in_library("Hello %s" % ', '.join(['%s'] * len(my_args)), my_args)