What does the \newline escape sequence mean in python?

前端 未结 2 584
时光取名叫无心
时光取名叫无心 2021-01-15 02:50

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 se

2条回答
  •  遥遥无期
    2021-01-15 03:50

    It refers to the actual newline character - the one with character code "10" (0x0a) - 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 \x0a on parsing the string. On \, the source string contains the \x0a character and that is replaced by an empty string.

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

提交回复
热议问题