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)
Answer from Martijn Pieters on Stack Overflow
🌐
CodeQL
codeql.github.com › codeql-query-help › python › py-percent-format-wrong-arguments
Wrong number of arguments for format — CodeQL query help documentation
Change the format to match the arguments and ensure that the right hand argument always has the correct number of elements. In the following example the right hand side of the formatting operation can be of length 2, which does not match the format string. def unsafe_format(): if unlikely_condition(): args = (1,2) else: args = (1, 2, 3) return "%s %s %s" % args · Python Library Reference: String Formatting.
Discussions

python - argparse for unknown number of arguments and unknown names - Stack Overflow
I'd like to fetch all parameters passed to sys.argv that have the format someprogram.py --someparameter 23 -p 42 -anotherparam somevalue. Result I'm looking for is a namespace containing all the More on stackoverflow.com
🌐 stackoverflow.com
July 10, 2018
Calling python function with an unknown number of arguments - Stack Overflow
And of course, you can combine the splat operator with the variable number of arguments: ... Sign up to request clarification or add additional context in comments. ... Use the splat operator(*) to pass and collect unknown number of arguments positional arguments. More on stackoverflow.com
🌐 stackoverflow.com
templates - Formatting using a variable number of .format( ) arguments in Python - Stack Overflow
I can't seem to figure out a straightforward way to make code that finds the number of items to format, asks the user for the arguments, and formats them into the original form. A basic example of... More on stackoverflow.com
🌐 stackoverflow.com
April 25, 2017
Trouble getting function to take unknown amount of arguments
You need to use args. Right now you're ignoring args, so there's no point in having the args parameter. You likely mean something like this: def fun(*args): parsed_args = [int(arg) for arg in args] # Use parsed_args balls = "1" fun(balls) I added the comprehension because args is a tuple of all the passed data, so you need to loop over that tuple. More on reddit.com
🌐 r/learnpython
3
6
November 26, 2022
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node67.html
Variable Number of Arguments
A similar technique can be used to create functions which can deal with an unlimited number of keyword/argument pairs. If an argument to a function is preceded by two asterisks, then inside the function, Python will collect all keyword/argument pairs which were not explicitly declared as arguments ...
🌐
SQLPad
sqlpad.io › tutorial › solving-pythons-not-enough-arguments-for-format-string-error
Solving Python's 'Not Enough Arguments for Format String' Error
April 29, 2024 - Break Down Complex Expressions: Instead of using a single complex format string, break it down into smaller, manageable parts. This makes it easier to ensure each part receives the correct number of arguments. Adopt F-Strings for Clarity: Introduced in Python 3.6, f-strings offer a more readable and concise way to format strings.
🌐
Python
python.earth › home › handling unknown number of arguments in python functions
Handling Unknown Number of Arguments in Python Functions - python.earth
March 24, 2023 - When working with Python, there may be situations where you need to handle an unknown number of arguments in a function. This can be achieved by using the *arguments syntax. The *arguments parameter is used when we want to pass an unknown number of unnamed arguments to a function.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › trouble getting function to take unknown amount of arguments
r/learnpython on Reddit: Trouble getting function to take unknown amount of arguments
November 26, 2022 -

So I have a function and it sometimes will need to take in a different amount of arguments. I'm trying something like this:

Def fun(*args):

balls = int(balls)

fun(balls)

But getting an error local variable not defined

But I can do this:

Def fun(*args):

Myballs = int(balls)

fun(balls)

I can't really see why the first one don't work and the second does and I just want the variable passed in with the same name.

What am I missing here?

🌐
Medium
medium.com › geekculture › python-power-variable-number-of-arguments-5c8c062039fb
Python Power: Variable Number of Arguments | by Al Williams | Geek Culture | Medium
October 1, 2021 - printf("%d",random_number); printf("%s: %d (%f)\n","Results:", count, average); The function doesn’t “know” how many arguments it should take, other than the first argument which then tells it how many other arguments to expect.
🌐
Medium
medium.com › @pivajr › pythonic-tips-functions-with-an-uncertain-number-of-parameters-f0f81c4465aa
Pythonic Tips: Functions with an Uncertain Number of Parameters | by Dilermando Piva Junior | Medium
March 10, 2025 - # Function that accepts an unknown number of arguments def sum_numbers(*args): total = 0 for x in args: total += x return total # Testing the Function print(sum_numbers(1, 2)) # Output: 3 print(sum_numbers(1)) # Output: 1 print(sum_numbers(1, ...
🌐
Stack Overflow
stackoverflow.com › questions › 55342352 › python-function-that-receives-an-unknown-number-of-arguments
Python function that receives an unknown number of arguments - Stack Overflow
March 25, 2019 - Example: mult can be called with no arguments, but multT() cannot. ... Which of those two forms is going to be most convenient and/or natural to the callers of your function? Note that it's easy to support both, as the built-in min() and max() functions do - if len(args) == 1: args = args[0]. ... Austin - But I could send a tuple with 0 or more elements. Isn't that the same as an unknown number of arguments?
🌐
Codefinity
codefinity.com › courses › v2 › 0825a584-2410-4fa5-94d9-db910028b828 › 86650855-2e35-44b1-9bb9-4028af363603 › 81262e3b-c1ce-448a-98ef-e174c97981b3
Learn *args | An Unknown Number of Arguments
*args is needed when we want to pass an unknown number of unnamed arguments. If we put * before the name of the variable, this name will take not one argument, but several.
🌐
GitHub
github.com › orgs › community › discussions › 77313
Creating a generic for an unknown number of argument of a Callable in python · community · Discussion #77313
November 29, 2023 - The generic for the arguments should be represented using a Tuple[Any, ...]. This indicates that the func parameter accepts any number of arguments, each of which can be of any type. The Any type is a placeholder for unknown or generic types.
🌐
CodeQL
codeql.github.com › codeql-query-help › python › py-call-wrong-number-class-arguments
Wrong number of arguments in a class instantiation — CodeQL query help documentation
The maximum number of arguments is the total number of parameters, unless the class __init__ method takes a varargs (starred) parameter in which case there is no limit. If there are too few arguments then check to see which arguments have been omitted and supply values for those.
🌐
Reddit
reddit.com › r/learnpython › unknown number of arguments
r/learnpython on Reddit: Unknown number of arguments
February 8, 2019 -

I would like my function to be ready to take single string argument and act on it or take list of strings and iterate on it and act on each element.

Doing this way:

def foo( args ):
    for arg in args:
        print( arg )

single = 'first'
multi = ['first', 'second', 'third']

foo( single )
foo( multi )

gives undesired result as it iterates over characters in may single string.

Doing alternative way:

def foo( *args ):
    for arg in args:
        print( arg )

single = 'first'
multi = ['first', 'second', 'third']

foo( single )
foo( multi )

also gives undesired result, it takes multi as one argument instead of three.

What is the best practice in this case?

🌐
Stack Abuse
stackabuse.com › variable-length-arguments-in-python-with-args-and-kwargs
Variable-Length Arguments in Python with *args and **kwargs
October 21, 2021 - Variable-length arguments, varargs for short, are arguments that can take an unspecified amount of input. When these are used, the programmer does not need to wrap the data in a list or an alternative sequence. In Python, varargs are defined using the *args syntax.
🌐
Stack Overflow
stackoverflow.com › questions › 72774834 › how-can-i-deal-with-an-unknown-number-of-system-arguments
python - How can I deal with an unknown number of system arguments? - Stack Overflow
I am writing code that will make a list of system arguments and I need it to work for a different number of arguments. this is what i have tried with a for loop: import sys seqs = [] for i in range...