What does the \newline escape sequence mean in python?

644
2
06-17-2022 02:43 AM
MubashshirHasan
New Contributor II

In the Python manual, I found the sequence \newline in a list of escape sequences. This article from Scaler demonstrated to me that it does not represent itself but is transformed into another character or set of characters that, like newline, may be difficult or impossible to describe directly.

I'm curious how and why it's used. This appears to be translated as 'n' + 'ewline' in my interpreter:

>>> print('\newline')

ewline
0 Kudos
2 Replies
DavidAnderson_1701
Occasional Contributor

It does almost what it says it does.  The \n is the escape sequence for printing a new line in a text string.  The rest of the string is what is to be printed

print ("\newline')

ewline

 Lets not forget the venerable \r for carriage return.  There are a few more shown here,

https://en.wikipedia.org/wiki/Escape_character

 

 

0 Kudos
Brian_Wilson
Occasional Contributor III

A backslash means the next character is special.

\n means newline

\t means tab

\\ means \ so for example, "C:\\Program Files" will be interpreted to mean "C:\Program Files".