working of \n in python
This question already has an answer here:
There are two functions that give an object's string representation, repr()
and str()
. The former is designed to convert the object to as-code string, while the latter gives user-friendly string.
When you input the variable name in the command line, repr()
is used, and n
character is shown as n
(as-code). When you use print
, str()
is used, and n
is shown as a new line (user-friendly).
By the way, str
is a bad name for a variable, as it's the same as the built-in.
When you do
st
It is somewhat similar to
"n"
where you expect the string n
in return, and not a new line.
In both cases, the print function is not called which can actually process what n
really means. That's why print 'HellonWorld'
gives different output than
>>> 'HellonWorld'
链接地址: http://www.djcxy.com/p/28326.html
上一篇: 变量和打印变量之间的区别
下一篇: 在Python中工作\ n