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

136,437

Solution 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
    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))
    

Solution 2

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

string = "C:\\Windows\Users\alexb"

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

output:

'C:\\\\Windows\\Users\\alexb'

Solution 3

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.

Solution 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)

Solution 5

try this. Based on what type of output you want. sometime you may not need single quote around printed string.

test = "qweqwe\n1212as\t121\\2asas"
print(repr(test)) # output: 'qweqwe\n1212as\t121\\2asas'
print( repr(test).strip("'"))  # output: qweqwe\n1212as\t121\\2asas
Share:
136,437
aescript
Author by

aescript

Updated on July 17, 2022

Comments

  • aescript
    aescript almost 2 years

    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

  • malhar
    malhar over 5 years
    Interestingly this does not work for \v >>> k = "\vsadfkjhkasdf\nasdfsadf" >>> r = r"{}".format(k) >>> r `'\x0bsadfkjhkasdf\nasdfsadf'``
  • shub
    shub almost 5 years
    I believe you really got the point. None of the above worked for me, when I needed to use re and variables in a re.sub call (with Windows paths, but is just a case among others). I solved (badly) re-escaping the strings: path = r"c:\this\is\a\test.txt" ; part_to_be_changed = r"c:\this" ; part_to_be_changed_reescaped = re.sub("\\\\", "\\\\\\\\", part_to_be_changed) ; new_part = "d:\\this_really" ; new_part_reescaped = re.sub("\\\\", "\\\\\\\\", new_part) ; new_string = re.sub(part_to_be_changed_reescaped, new_part_reescaped, path) ; print(new_string) ; >> d:\this_really\is\a\test.txt
  • kumar chandraketu
    kumar chandraketu almost 4 years
    Please specify the reason for downvote. i just used this approach yesterday for my application.
  • Alper
    Alper over 3 years
    this works for variable = r"{}".format(another_variable) like a magic
  • user07
    user07 over 3 years
    It does not work: ` str1 = 'dfdf\"dfdfd' str2 = r"{}".format(str1) print(str2) ` Output: dfdf"dfdfd
  • Farhan Hai Khan
    Farhan Hai Khan over 2 years
    great answer! works for me, however could you explain why this works and why the others don't? Is this a rendering problem in python??