Python Tutorial

Python Escape Characters Explained Using repr(), Using “r/R”

Escape Characters in Python Explained

Escape characters in Python are characters that are commonly used to complete specific jobs, and their use in code instructs the compiler to perform the appropriate action for that character.

Example:

'\n'  -->  Leaves a line

'\t'  -->  Leaves a space 

However, in some circumstances, it is preferable not to resolve escapes, in which case the complete unresolved string must be shown. These can be accomplished in a variety of ways.

Using repr()

This method returns a string in its printable format, which means the escape sequences are not resolved.

Using “r/R”

Adding "r" or "R" to the target string causes the string to be repr()ed internally, which prevents escape characters from being resolved.

QUIZ!

quiz-img
Did you find this article helpful?