Print raw string from variable? (not getting the answers)

Posted on

Question :

Print raw string from variable? (not getting the answers)

I’m trying to find a way to print a string in raw form from a variable. For instance, if I add an environment variable to Windows for a path, which might look like 'C:\WindowsUsersalexb', I know I can do:


But I cant put an r in front of a variable…. for instance:

test = 'C:\WindowsUsersalexb'

Clearly would just try to print rtest.

I also know there’s

test = 'C:\WindowsUsersalexb'

But this returns 'C:\Windows\Usersx07lexb'
as does

test = 'C:\WindowsUsersalexb'

So I’m wondering if there’s any elegant way to make a variable holding that path print RAW, still using test? It would be nice if it was just


But its not

Asked By: aescript


Answer #1:

I had a similar problem and stumbled upon this question, and know thanks to Nick Olson-Harris’ answer that the solution lies with changing the string.

Two ways of solving it:

  1. Get the path you want using native python functions, e.g.:

    test = os.getcwd() # In case the path in question is your current directory

    This makes it platform independent and it now works with .encode. If this is an option for you, it’s the more elegant solution.

  2. If your string is not a path, define it in a way compatible with python strings, in this case by escaping your backslashes:

    test = 'C:\Windows\Users\alexb\'
Answered By: OlavRG

Answer #2:

You can’t turn an existing string “raw”. The r prefix on literals is understood by the parser; it tells it to ignore escape sequences in the string. However, once a string literal has been parsed, there’s no difference between a raw string and a “regular” one. If you have a string that contains a newline, for instance, there’s no way to tell at runtime whether that newline came from the escape sequence n, from a literal newline in a triple-quoted string (perhaps even a raw one!), from calling chr(10), by reading it from a file, or whatever else you might be able to come up with. The actual string object constructed from any of those methods looks the same.

Answered By: Nick Olson-Harris

Answer #3:

In general, to make a raw string out of a string variable, I use this:

string = "C:\WindowsUsersalexb"

raw_string = r"{}".format(string)


Answered By: Samer Alkhabbaz

Answer #4:

I know i’m too late for the answer but for people reading this I found a much easier way for doing it

myVariable = 'This string is supposed to be raw '
print(r'%s' %myVariable)
Answered By: Jimmynoarms

Answer #5:

Get rid of the escape characters before storing or manipulating the raw string:

You could change any backslashes of the path ” to forward slashes ‘/’ before storing them in a variable. The forward slashes don’t need to be escaped:

>>> mypath = os.getcwd().replace('\','/')  
>>> os.path.exists(mypath)  
Answered By: DrawT

Answer #6:

Your particular string won’t work as typed because of the escape characters at the end “, won’t allow it to close on the quotation.

Leave a Reply

Your email address will not be published. Required fields are marked *