Search code examples
pythonstring-interpolationsplat

Python string interpolation with splat operator?


I have a large (~10 elements) list of integers that I wish to interpolate into a string. This seems like an ideal use case for the splat operator, so I'd like to be able to do something like this:

"""[latex]$\begin{bmatrix}
%d & %d \\ %d & %d\end{bmatrix}
\times
\begin{bmatrix}
%d & %d \\ %d & %d
\end{bmatrix} $[/latex]""" % (*lst)
                              ^^^^ SyntaxError: invalid syntax

What is a clean, syntactically valid way to achieve this?


Solution

  • str.__mod__ takes a tuple.

    '''...''' % tuple(lst)