Strings

Michael Chermside mcherm at destiny.com
Fri Jan 18 12:43:02 EST 2002


> 
> i was a bit anoyed about the double backslash, then i had another 
> idea:

    [..snip...]

> if there another shorter way of just printing one backslash, tell me 
> please.


Actually, the problem with the double backslash makes sense once you 
understand the reason.

The problem is that most of the time, you want to be able to put 
"special" characters into strings -- things like newlines, tabs, control 
characters -- without necessarily typing them into the program code. We 
do this with special backslash codes (just like C, Java, and others):


   >>> funnyString = "tab-[\t] newline-[\n] null_char-[\0]"
   >>> print funnyString
   tab-[	] newline-[
   ] null_char-[ ]


Of course, this means that any time it sees a \ before a "n" or any of 
the other specially-interpreted characters, it gets turned into a 
newline (or whatever). So there's a special code for putting in normal 
backslashes... the backslash-backslash.



    >>> strWithBackslash = "backslash-[\\]"
    >>> print strWithBackslash
    backslash-[\]


This is useful whenever you want to put a backslash into a string. But 
sometimes you want to put LOTS of backslashes in a string, or you REALLY 
just don't want to have to write out that extra backslash (perhaps you 
think it might be confusing to the person reading your program). In that 
case, Python allows you to turn OFF the special use of backslashes by 
putting the letter 'r' in front of the string (called a "raw" string). 
This means that backslashes will be normal (no need to double them), but 
you won't get automatic conversion of \n to newline and the others. (Of 
course, that might be exactly what you WANT!) Here's how it works:

     >>> dosFilename = r"C:\Documents\personal\python\test.py"
     >>> print dosFilename
     C:\Documents\personal\python\test.py

This feature is most often used with regular expressions, but you're 
allowed to use it anywhere you like. Unfortunately, (for what you're 
trying to accomplish) there's ONE special interpretation that's still 
used even in raw strings: a quote character (" or ') of the kind used to 
start the string is just another character (instead of ending the 
string) if it follows an odd number of backslashes. The reason for this 
odd rule is simply that it makes it much easier to write regular 
expressions. The effect, though, is that there's no way to write a raw 
string which ends in an odd number of backslashes. So if you want a 
string consisting of a single backslash, the only ways to write it are 
"\\" and '\\' (or the triple quote versions).

Your solution (change all '/' to '\/', then remove all '/') works, but 
it is a bit less elegant (and certainly takes longer to run) than the 
original version. But if you want to make things more readable, then you 
can simply give things a name:

     >>> backslash = "\\" # a single backslash
     >>> dir = "C:/Files/Games/Pygame"
     >>> dir = dir.replace('/', backslash)
     >>> print dir
     C:\Files\Games\Pygame

Happy pythoning!

-- Michael Chermside







More information about the Python-list mailing list