简体   繁体   English

使用splat运算符进行Python字符串插值?

[英]Python string interpolation with splat operator?

I have a large (~10 elements) list of integers that I wish to interpolate into a string. 我有一个很大的整数列表(〜10个元素),我希望将它们插入一个字符串中。 This seems like an ideal use case for the splat operator, so I'd like to be able to do something like this: 对于splat运算符来说,这似乎是理想的用例,因此我希望能够执行以下操作:

"""[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? 有什么干净的,语法上有效的方法可以实现这一目标?

str.__mod__带一个元组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM