格式化字符串时多次插入相同的值 [英] Inserting the same value multiple times when formatting a string

查看:115
本文介绍了格式化字符串时多次插入相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这种形式的字符串

$ $ $ $ $ $ $ $ $ $'$'$ b $ string ='%s hello world %s hello world%s'%(s,s,s)

字符串中的所有%s具有相同的值(即s)。
有没有更好的写作方法? (而不是列出s三次)

解决方案

您可以使用高级字符串格式化,可在Python 2.6和Python 3.x中找到:

<$ p格式(传入)
传入='仲裁'$ b $结果='{0} hello世界{0} hello world {0} >


I have a string of this form

s='arbit'
string='%s hello world %s hello world %s' %(s,s,s)

All the %s in string have the same value (i.e. s). Is there a better way of writing this? (Rather than listing out s three times)

解决方案

You can use advanced string formatting, available in Python 2.6 and Python 3.x:

incoming = 'arbit'
result = '{0} hello world {0} hello world {0}'.format(incoming)

这篇关于格式化字符串时多次插入相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆