我想知道给定格式字符串所需的参数数量。例如,"%s is my name, and %s is my hair color"
将需要两个参数。我可以发现%
字符串中出现的次数,但是如果我真的想要%
在字符串中显示a ,而这必须%%
表明这一点,那将是行不通的。它也似乎一点也不优雅。有没有更好的办法?
我想出的最简单的方法是:
my_string = "%s is my name, and %s is my hair color"
my_string.count('%') - 2 * my_string.count('%%')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句