如何有选择地避免Python字符串中的百分比(%)?

问题:

我有以下代码

test = "have it break."
selectiveEscape = "Print percent % in sentence and not %s" % test

print(selectiveEscape)

我想得到输出:

Print percent % in sentence and not have it break.

实际发生的是

    selectiveEscape = "Use percent % in sentence and not %s" % test
TypeError: %d format: a number is required, not str

回答:

>>> test = "have it break."
>>> selectiveEscape = "Print percent %% in sentence and not %s" % test
>>> print selectiveEscape
Print percent % in sentence and not have it break.

 
 Code问答: codewenda.com
Stackoverflow:How can I selectively escape percent (%) in Python strings?

发表评论

电子邮件地址不会被公开。 必填项已用*标注

40 − 30 =