Menu Close

How to selectively escape percent (%) in Python strings?

Sometimes, we want to selectively escape percent (%) in Python strings.

In this article, we’ll look at how to selectively escape percent (%) in Python strings.

How to selectively escape percent (%) in Python strings?

To selectively escape percent (%) in Python strings, we put % before the % sign to escape it.

For instance, we write

elective_escape = "Print percent %% in sentence and not %s" % test

to escape the first % with %%.

Then we print the string, one % will be printed.

Conclusion

To selectively escape percent (%) in Python strings, we put % before the % sign to escape it.

Posted in Python, Python Answers