what does backward-slash b do in Python?

前端 未结 2 2058
逝去的感伤
逝去的感伤 2020-12-07 00:45

what is the purpose of backward-slash b in python, I ran print \"\\\"foo\\bar\" in the python interpreter and got this result:

>>>p         


        
相关标签:
2条回答
  • 2020-12-07 00:59

    The \b is a back space character

    \b  ASCII Backspace (BS)
    

    If you want to print the string \foo\bar do this:

    >>> print r"\foo\bar"
    \foo\bar
    

    This utilizes the raw strings available in python.

    String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences

    0 讨论(0)
  • 2020-12-07 01:16

    See the string literal documentation:

    \b ASCII Backspace (BS)

    It produces a backspace character. Your terminal backspaced over the second o when printing that character.

    0 讨论(0)
提交回复
热议问题