Ways to Print Escape Characters in Python
Last Updated :
11 Mar, 2025
In Python, escape characters like \n (newline) and \t (tab) are used for formatting, with \n moving text to a new line and \t adding a tab space. By default, Python interprets these sequences, so I\nLove\tPython will display "Love" on a new line and a tab before "Python." However, if you want to display the escape characters themselves as raw text (e.g., I\nLove\tPython), you can use different methods.
Using repr()
repr() function efficiently preserves escape characters in their raw form, making it the best way to display them without interpretation. It returns the official string representation, ensuring escape sequences remain visible.
Python
s = "I\nLove\tPython"
print(repr(s))
Explanation: repr()
ensures escape characters remain visible rather than being interpreted.
Using raw strings(r '')
A raw string prevents Python from interpreting escape sequences, making it useful when working with file paths and regular expressions.
Python
s = r"I\nLove\tPython"
print(s)
Explanation: The r prefix ensures \n and \t are displayed as-is, making raw strings useful for file paths and regex.
Using double backslashes(\\)
By manually doubling backslashes, escape sequences are prevented from being interpreted. This approach is straightforward but requires additional effort when writing strings.
Python
s = "I\\nLove\\tPython"
print(s)
Explanation: The \\ ensures \n and \t are treated as literal text instead of formatting commands.
Using encode() and decode()
Encoding a string with unicode_escape and decoding it back ensures escape characters remain intact. This method is useful for data processing but adds computational overhead.
Python
s = "I\nLove\tPython"
print(s.encode("unicode_escape").decode("utf-8"))
Explanation: Converts escape characters into their escaped forms (\\n and \\t), making them visible.
Using json.dumps()
json.dumps() function preserves escape sequences in JSON format, making it useful for data serialization. However, it is less efficient due to additional processing.
Python
import json
s = "I\nLove\tPython"
print(json.dumps(s))
Explanation: Converts the string into a JSON-compliant format, retaining escape sequences.
Similar Reads
How To Print Unicode Character In Python? Unicode characters play a crucial role in handling diverse text and symbols in Python programming. This article will guide you through the process of printing Unicode characters in Python, showcasing five simple and effective methods to enhance your ability to work with a wide range of characters Pr
2 min read
Python Escape Characters In Python, escape characters are used when we need to include special characters in a string that are otherwise hard (or illegal) to type directly. These are preceded by a backslash (\), which tells Python that the next character is going to be a special character. Theyâre especially helpful for:For
2 min read
Different Ways to Escape percent (%) in Python strings In Python, the percent sign (%) plays a crucial role, especially in string formatting and as a modulo operator. However, there are times when we want to use the percent sign as a literal character in a string rather than as a placeholder for formatting. In such cases, knowing how to escape the perce
4 min read
How to print Odia Characters and Numbers using Python? Odia(ଓଡ଼ିଆ) is an Indo-Aryan language spoken in the Indian state of Odisha. The Odia Script is developed from the Kalinga alphabet, one of the many descendants of the Brahmi script of ancient India. The earliest known example of Odia language, in the Kalinga script, dates from
2 min read
Converting an Integer to ASCII Characters in Python In Python, working with integers and characters is a common task, and there are various methods to convert an integer to ASCII characters. ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents text in computers. In this article, we will explore s
2 min read