What does the \newline escape sequence mean in python?

痞子三分冷 提交于 2019-12-02 08:56:15

问题


I found the sequence \newline in a list of escape sequences in the python documentation. I wonder how it is used and for what. At least in my interpreter it seems this is just interpreted as '\n' + 'ewline':

>>> print('\newline')

ewline

回答1:


It refers to the actual newline character - the one with character code "16" (0x10) - not the text sequence "newline".

So, an example is like:

print("a\
b")

Here, the backslash is succeeded by the newline, inside a string, and what is printed is just "ab" with nothing apart.

it differs from \n - in here, the characer following the backslash is n (0x6e), and this sequence is translated to \x10 on parsing the string. On \<newline>, the source string contains the \x10 character and that is replaced by an empty string.

Maybe the documentation on that page would be more clear if it would read \<newline> instead of just \newline.




回答2:


The documentation you are alluding to is explaining how a backslash followed by a literal newline is ignored, as if the next line were physically joined with the line on which the starting backslash was found.

The string \newline' has no special meaning; it is exactly what you say you think it is.



来源:https://stackoverflow.com/questions/48693600/what-does-the-newline-escape-sequence-mean-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!