我有以下代码
test = "have it break."
selectiveEscape = "Print percent % in sentence and not %s" % test
print(selectiveEscape)
Run Code Online (Sandbox Code Playgroud)
我想获得输出:
Print percent % in sentence and not have it break.
Run Code Online (Sandbox Code Playgroud)
实际发生了什么:
selectiveEscape = "Use percent % in sentence and not %s" % test
TypeError: %d format: a number is required, not str
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用lazy参数构建一个格式字符串,例如我需要像:
"%s \%s %s" % ('foo', 'bar') # "foo %s bar"
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?