Open In App

Escape From \n Newline Character in Python

Last Updated : 05 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to escape from \n newline characters in Python.

Escape sequence interpretation is a common problem that arises anytime a program tries to process a string containing an escape character. Most of the time, the effects produced by the escape sequences are desirable but sometimes unwanted. This article will teach you how to escape a newline character in Python.

New line character

A newline character or \n is a control character for Line feed, used to make the cursor move from the current line to the next line. The newline character produces such an effect only in the programming language (or inside it). i.e., To produce the cursor move to next line effect on Windows, a \r along with a \n needs to be provided. But the language is equipped with utility that allows platform-independent methods of working across these variations. 

Escaping escape sequences

The escape sequence interpretation is a default in string in Python. Most of the time, the effect is desired as it is required to beautify the output. But sometimes may not be such. Escaping the newline character could be done before in two ways:

  1. Using raw strings
  2. Encoding the string  

Using raw strings

Raw strings are used when the string is at hand before preprocessing. Raw strings are very much helpful in defining paths to directories and resources over the internet. Raw strings are identified/associated by the presence of character r (or R) before the first quote of a string. The syntax for this is as follows:

r'_string_'

or 

r"_string_"

The following example will demonstrate how to use the raw strings. 

Python3




# A default python string containing a newline character
string = "Hello\nWorld"
  
# A raw string of the data
string_raw = r"Hello\nWorld"
  
# Displaying the data in both the strings
print(string)
print(string_raw)


Output

Hello
World
Hello\nWorld

Explanation:

A regular string is defined containing the text Hello\nWorld in it. A raw string is defined (identified by the r before the first quote) and assigned the same text as the previous string. Both strings are displayed. 

The data within the first string was displayed in two lines because of the new line interpretation that occurred due to the presence of the \n character. This lead to a line break at the point where the character has been located (middle), and the rest of the data appears on the next line. This has not happened in the string_raw (raw string) due to the raw string auto-escaping any escape sequences in the string by default.

 Encoding the string  

The same results as the aforementioned example could be obtained by encoding the string to Unicode and decoding it later to its original encoding system. This allows all the characters from all code points to be encompassed within the string without producing any artifacts. As the Unicode system allows a unique representation of all characters, initial conversion to Unicode automatically escapes any escape sequence within the string. This conversion transforms the string into a byte, which needs to be reverted (while preserving the mapping) back to a string. The byte string is decoded to the UTF-8 (Unified Text Format 8bits) encoding. This encoding could represent all characters in 1 to 4 bytes. Ultimately, we obtain the original string in which all the escape sequences are handled. 

Python3




# A default python string containing a newline character
string = "Hello\nWorld"
  
# A String firstly encoded to unicode_escape
# Later decoded to UTF-8 
string_raw = "Hello\nWorld".encode("unicode_escape").decode("utf-8")
  
# Displaying the data in both the strings
print(string)
print(string_raw)


Output

Hello
World
Hello\nWorld

Explanation: 

A regular string is defined containing the text Hello\nWorld in it. Another string is defined and assigned the same text as the previous string, where the text has been firstly encoded to Unicode (to produce a Unicode literal) and later decoded to UTF-8 (to revert to string). In the end, the strings are displayed where the effect produced by the encoded string is apparent as the newline character is treated as a regular pair of characters, then an escape sequence. 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads