[Tutor] What does a \ do?
Steven D'Aprano
steve at pearwood.info
Sun Sep 23 17:37:49 CEST 2012
On 24/09/12 00:47, Gina wrote:
> What does a \ do?
Depends on the context.
In Python code, if you end the line with a backslash, Python will continue
the line of code into the next. So:
print 23 + \
x
is the same as:
print 23 + x
Inside a string, \ is used to start an "escape sequence" which has special
meaning to Python. For example, the string "x\ny" is *not* X BACKSLASH N Y,
instead it is X NEWLINE Y: the \n means "newline".
Some of the escape sequences are:
\n newline
\t tab
\r carriage return
\f form feed
\\ backslash
and others.
Inside a regular expression ("import re"), backslashes are used to specify
what you want to search for. For example, \d means to search for any decimal
digit 0...9.
Finally, inside Windows file names, backslash is used to separate folders
from subfolders. You can also use forward slashes for the same thing. So:
My Documents\new folder\another folder\file.txt
is a Windows file pathname.
--
Steven
More information about the Tutor
mailing list