How to implement multiple% s list expansion in python

suppose there is a string like this:

fmt = %s,%s,%s

and the corresponding data is [1JI 2jue 3]. Now you want to expand the list automatically when formatting:

fmt = fmt % (*[1,2,3])

you can get the result 1 , instead of grammatical errors.

Mar.01,2021

can be implemented in the following way,

fmt = fmt % tuple([1,2,3])

so you get the data results without syntax errors.


give it a try. You can't use an asterisk expression to expand a list into a tuple , which will report

.
SyntaxError: can't use starred expression here

but you can

fmt% tuple ([1,2,3])


add multiple commas to indicate tuple

fmt = '%s, % s, % s'
fmt = fmt % (*[1, 2, 3], )
print(fmt)
Menu