How can I selectively escape percent (%) in Python strings? How can I selectively escape percent (%) in Python strings? python python

How can I selectively escape percent (%) in Python strings?


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


Alternatively, as of Python 2.6, you can use new string formatting (described in PEP 3101):

'Print percent % in sentence and not {0}'.format(test)

which is especially handy as your strings get more complicated.


try using %% to print % sign .


matomo