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

25,212

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

"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, ...):

>>> "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 join in the interpolation argument list, use a join to create the formatting string instead:

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

Related videos on Youtube

Gerard
Author by

Gerard

Lover of music, food and open source. Always looking for something new to learn, whatever the subject.

Updated on August 23, 2020

Comments

  • Gerard
    Gerard over 3 years

    We know that formatting one argument can be done using one %s in a string:

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

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

    >>> "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 %s equal to the number of arguments to format? it would be very cool if something like this exists:

    >>> "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 need to do it with the %s string formatting operator.

    UPDATE:

    It needs to be with the %s because 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.

    So I need to call it:

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

    Thanks for your help!

    • Martijn Pieters
      Martijn Pieters over 10 years
      Why do you need to use the %s operator? That makes little sense; use str() instead.
    • abarnert
      abarnert over 10 years
      How would the "cool operator here" specify how to connect up the values? You don't want it to just print out Hello JohnJoeMary, or Hello ("John", "Joe", "Mary"), do you? You might want Hello John, Joe, Mary or Hello John, Joe, and Mary or Hello John, Joe and Mary, or a million other things; there's no way a single operator could let you do all of those.
    • Oli
      Oli over 10 years
      You should still be able to call it with function_in_library("Hello %s", ', '.join(["John", "Joe", "Mary"])
    • Gerard
      Gerard over 10 years
      Sorry, I explained the library function wrong. Anyway, I may not join the arguments before passing them to the function :\
  • Gerard
    Gerard over 10 years
    Though you are right, I think I didn't explain my question clear enough. I missed to state that I'm not making the formatting, an existing function is. Sorry, but thanks. I'll update my question.
  • Gerard
    Gerard over 10 years
    Sorry I may not join the arguments, each one needs to be processed by the library function before formatting. Thanks.
  • Martijn Pieters
    Martijn Pieters over 10 years
    That is a rather arbitrary restriction; in that case build the %s string elements using a separate join: "Hello %s" % ', '.join(['%s'] * len(my_args)). Note the irony here; you are creating the string using the exact same technique.
  • Gerard
    Gerard over 10 years
    Yeah, I use that example in my question, but I say I'd rather not to do so if there was another way around. Basically my question is if a somehow-magic operator even exists or I if must build the string myself? But I guess the second is the answer I'm getting :/ Sorry if this sounds rude, I don't mean to.
  • Martijn Pieters
    Martijn Pieters over 10 years
    You must build the string yourself.
  • AN88
    AN88 almost 8 years
    The last example is exactly what I needed to incorporate 'and' and 'or' before the last arg. Thanks!