python字符串格式 [英] python string formatting
本文介绍了python字符串格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这工作:
<$ p $
$ $ b
但是,这不是:
$ p $ f $ ='bar'
print('{{ 0}}'。format(foo))
所需输出:
解决方案
b
>>> foo ='bar'
>>> print('{{{0}}'。format(foo))
'{bar}'
将外部的一对 {{
和}}
复制到输出中, code> {0} 被解释为替代。
I need some help with the Python's new string formatter. Basically, I want the outer curly braces to not escape the string replacement field.
This works:
foo = 'bar'
print '{%s}' % foo
But, this doesn't:
foo = 'bar'
print('{{0}}'.format(foo))
Desired output:
'{bar}'
解决方案
It looks like you want:
>>> foo = 'bar'
>>> print('{{{0}}}'.format(foo))
'{bar}'
The outer pair of doubled {{
and }}
are copied literally to the output, leaving {0}
to be interpreted as a substitution.
这篇关于python字符串格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文