Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does backward-slash b do in Python?

Tags:

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 
like image 448
hky404 Avatar asked Jul 31 '14 17:07

hky404


People also ask

What does forward slash do in Python?

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.

What is a backslash called in Python?

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.


2 Answers

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.

like image 160
Martijn Pieters Avatar answered Oct 14 '22 10:10

Martijn Pieters


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

like image 22
Andy Avatar answered Oct 14 '22 11:10

Andy