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:
print(r'C:\WindowsUsersalexb')
But I cant put an r
in front of a variable.... for instance:
test = 'C:\WindowsUsersalexb'
print(rtest)
Clearly would just try to print rtest
.
I also know there's
test = 'C:\WindowsUsersalexb'
print(repr(test))
But this returns 'C:\Windows\Usersx07lexb'
as does
test = 'C:\WindowsUsersalexb'
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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…