如何在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.

以上是 如何在Python字符串中有选择地转义百分比(%)? 的全部内容, 来源链接: utcz.com/qa/415034.html

回到顶部