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
Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error.
In Python strings, the backslash “ ” is a special character, also called the “escape” character. It is used in representing certain whitespace characters: “\t” is a tab, “\n” is a new line, and “\r” is a carriage return.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With