Quoting backslashes in Python string literals

前端 未结 4 674
独厮守ぢ
独厮守ぢ 2020-11-22 16:21

I have a string that contains both double-quotes and backslashes that I want to set to a variable in Python. However, whenever I try to set it, the quotes or slashes are eit

4条回答
  •  醉梦人生
    2020-11-22 16:57

    You're being mislead by output -- the second approach you're taking actually does what you want, you just aren't believing it. :)

    >>> foo = 'baz "\\"'
    >>> foo
    'baz "\\"'
    >>> print foo
    baz "\"
    

    Incidentally, there's another string form which might be a bit clearer:

    >>> print r'baz "\"'
    baz "\"
    

提交回复
热议问题