What is the purpose of backward-slash b in python? I ran print "\"foo\bar"
in the Python interpreter and got this result:
>>> print "\"foo\bar"
"foar
What is the purpose of backward-slash b in python? I ran print "\"foo\bar"
in the Python interpreter and got this result:
>>> print "\"foo\bar"
"foar
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.
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
© 2022 - 2024 — McMap. All rights reserved.