Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

backward slash followed by a number in python strings

I've encountered a problem in Python when dealing with backward slashes followed by numbers inside a string. I use windows OS environment.

This becomes especially annoying when you have numbers in the beginning of a name in a directory.

Ex: "P:\70_parseFile\80_FileDir\60_FA_050"

This was a discovery for me that you can create special characters if you do "\1", "\2", "\3"... and so on. As wonderful as this seems, I have to ask on how to go about turning this off, or what other different string function is there that doesn't have this special feature?

Thanks, all!

like image 808
AlexG Avatar asked Mar 12 '13 20:03

AlexG


1 Answers

You have two choices:

  • Backslash those backslashes:

    "P:\\70_parseFile\\80_FileDir\\60_FA_050"
    
  • Use a raw string, in which the backslash loses its "special meaning"

    r"P:\70_parseFile\80_FileDir\60_FA_050"
    
like image 137
icecrime Avatar answered Nov 14 '22 22:11

icecrime