[Tutor] path names

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Feb 22 13:56:55 EST 2004

> In Python code a \ (backslash) escapes the letter in front of it.
> instance in regular expressions ....
> \d{,3}.\d{,3}.\d{,3}.\d{,3} # match an IP address
> will do odd things because the periods are not escaped.

And just to emphasise the point its not just egular expressions
but any string. Thus we can create a string with a newline in
the middle like this:

>>> print "This line splits\nover two lines"
This line splits
over two lines

Notice the \n?

Which is OK except if you want to put a backslash itself in
the string - as we do with a Windows path. The solution is to
use a double backslash wjere the first is the usual escape
character and the second to tell Python that the special
char is a backslash...

>>> Print "This line has a tab\t and backslash(\\)"
This line has a tab     and backslash(\)


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list