Skip to main content
 首页 » 编程设计

python之如何将字符串插入到另一个具有相同多次出现的字符串中

2025年05月04日22telwanggs

如何简化下面的语法?

three='Three' 
result='One-Two-%s, One-Two-%s, One-Two-%s'%(three,three,three)) 

稍后编辑:

另一个惊喜:得到 KeyError: 'border' :

color='#262626' 
style="QProgressBar{background-color: {ph:s}}".format(ph=color) 

请您参考如下方法:

>>> three = 'Three' 
>>> result = 'One-Two-{ph:s}, One-Two-{ph:s}, One-Two-{ph:s}'.format(ph=three) 
>>> print result 
One-Two-Three, One-Two-Three, One-Two-Three 

编辑:

>>> style="QProgressBar{{background-color: {ph:s}}}".format(ph=color) 
>>> print style 
QProgressBar{background-color: #262626}