Using repr() with escape sequences

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Feb 23 10:53:29 EST 2006


On Thu, 23 Feb 2006 07:32:36 -0800, nummertolv wrote:

> Hi,
> 
> My application is receiving strings, representing windows paths, from
> an external source. When using these paths, by for instance printing
> them using str() (print path), the backslashes are naturally
> interpreted as escape characters.
> 
>>>> print "d:\thedir"
> d:	hedir

No. What is happening here is not what you think is happening.

> The solution is to use repr() instead of str():

The solution to what? What is the problem? The way the strings are
DISPLAYED is surely not the issue, is it?

>>>> print repr("d:\thedir")
> 'd:\thedir'


You have created a string object: "d:\thedir"

That string object is NOT a Windows path. It contains a tab character,
just like the print statement shows -- didn't you wonder about the large
blank space in the string?

Python uses backslashes for character escapes. \t means a tab character.
When you enter "d:\thedir" you are embedding a tab between the colon and
the h.

The solutions to this problem are:

(1) Escape the backslash: "d:\\thedir"

(2) Use raw strings that don't use char escapes: r"d:\thedir"

(3) Use forward slashes, and let Windows automatically handle them:
"d:/thedir"

However, if you are receiving strings from an external source, as you say,
and reading them from a file, this should not be an issue. If you read a
file containing "d:\thedir", and print the string you have just read, the
print statement uses repr() and you will see that the string is just
what you expect:

d:\thedir

You can also check for yourself that the string is correct by looking at
its length: nine characters.


-- 
Steven.




More information about the Python-list mailing list