Python 文字列でパーセント (%) を選択的にエスケープするにはどうすればよいでしょうか? 質問する

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

ベストアンサー1

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

おすすめ記事