Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\\Windows\Users\alexb\', I know I can do:

print(r'C:\\Windows\Users\alexb\') 

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

test = 'C:\\Windows\Users\alexb\' print(rtest) 

Clearly would just try to print rtest.

I also know there's

test = 'C:\\Windows\Users\alexb\' print(repr(test)) 

But this returns 'C:\\Windows\\Users\x07lexb' as does

test = 'C:\\Windows\Users\alexb\' print(test.encode('string-escape')) 

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

print(raw(test)) 

But its not

like image 338
aescript Avatar asked Sep 09 '13 21:09

aescript


People also ask

How do I convert a raw variable to a string?

In Python, when you prefix a string with the letter r or R such as r'...' and R'...' , that string becomes a raw string. Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters.

How do you return a raw string in Python?

Convert normal strings to raw strings with repr()Use the built-in function repr() to convert normal strings into raw strings. The string returned by repr() has ' at the beginning and the end. Using slices, you can get the string equivalent to the raw string.


2 Answers

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 print(repr(test)) 

    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\\' print(repr(test)) 
like image 61
OlavRG Avatar answered Oct 08 '22 12:10

OlavRG


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.

like image 44
Nick Olson-Harris Avatar answered Oct 08 '22 12:10

Nick Olson-Harris