Python raw strings and trailing backslash

前端 未结 4 1731
刺人心
刺人心 2020-12-03 06:49

I ran across something once upon a time and wondered if it was a Python \"bug\" or at least a misfeature. I\'m curious if anyone knows of any justifications for this behavi

4条回答
  •  眼角桃花
    2020-12-03 07:24

    Another way to workaround this is:

     >>> print(r"Raw \with\ trailing backslash\ "[:-1])
     Raw \with\ trailing backslash\
    

    Updated for Python 3 and removed unnecessary slash at the end which implied an escape.

    Note that personally I doubt I would use the above. I guess maybe if it was a huge string with more than just a path. For the above I'd prefer non-raw and double up the slashes.

提交回复
热议问题