[Tutor] What does a \ do?
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 + \
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:
\r carriage return
\f form feed
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
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.
More information about the Tutor