Work \ n in python

I am currently running python. Speaking of escape sequences, they talked about "\n", used to print lines on a new line. But when it is used in the following ways, why do I get a different output

>>> st = "Hello\nWorld"
>>> st
'Hello\nWorld'

But if I do

>>> print st
Hello
World
+4
source share
2 answers

There are two functions that provide a representation of the string of an object, repr()and str(). The former is intended to convert the object to a string with code, while the latter provides a user-friendly string.

, repr(), \n \n (as-code). print, str(), \n ( ).


, str - , .

+5

st

"\n"

\n , .

, , \n. print 'Hello\nWorld' ,

>>> 'Hello\nWorld'
0

Source: https://habr.com/ru/post/1598490/


All Articles