How to match a new line character in Python raw string

前端 未结 4 1440
無奈伤痛
無奈伤痛 2020-12-13 13:08

I got a little confused about Python raw string. I know that if we use raw string, then it will treat \'\\\' as a normal backslash (ex. r\'\\n\' wo

4条回答
  •  失恋的感觉
    2020-12-13 13:39

    The simplest answer is to simply not use a raw string. You can escape backslashes by using \\.

    If you have huge numbers of backslashes in some segments, then you could concatenate raw strings and normal strings as needed:

    r"some string \ with \ backslashes" "\n"
    

    (Python automatically concatenates string literals with only whitespace between them.)

    Remember if you are working with paths on Windows, the easiest option is to just use forward slashes - it will still work fine.

提交回复
热议问题