Strings

Quinn Dunkan quinn at hork.ugcs.caltech.edu
Fri Jan 18 19:00:34 EST 2002


On Fri, 18 Jan 2002 15:12:25 -0000, maximilianscherr
<MaximilianScherr at T-Online.de> wrote:
>Thanks to both of you,
>
>i was a bit anoyed about the double backslash, then i had another 
>idea:
>
>dir = "C:/Files/Games/Pygame"
>dir = dir.replace("/", "\/")<- not important what the second char. is!

Um, did you actually try it?  It is important what the second char is.

>dir = dir.replace("/", "")

The previous two lines replace all '/'s with '\/', and then delete all '/'s.
You'd be better of with a simple "dir.replace('/', '\\')".

>print dir

This last line is the only one needed to do what you want.  The *only* time
you will see \\s is when calling repr() on a string.  The interactive python
prompt calls repr() to show you values, so you'll see \\s at the prompt, but
that's purely cosmetic and only happens at the prompt.  The double backslashes
aren't actually in the string.

>
>output: C:\Files\Games\Pygame :)
>
>if there another shorter way of just printing one backslash, tell me 
>please.

Any way of printing a string *except* by typing its name at the '>>>' prompt
will print just one backslash.

Also I gather many windows functions will accept forward slashes too, but
apparently it's inconsistent about that, so you may be better off using
backslashes.  If you want to write a string literal path, it may be easiest to
say:

dir = r'C:\Files\Games\Pygame'
os.chdir(dir)
etc.



More information about the Python-list mailing list