Convert regular Python string to raw string

I have a string s, its contents are variable. I'd like to make it a raw string. How do I go about this?

Something similar to the r'' method.


Solution 1:

i believe what you're looking for is the str.encode("string-escape") function. For example, if you have a variable that you want to 'raw string':

a = '\x89'
a.encode('unicode_escape')
'\\x89'

Note: Use string-escape for python 2.x and older versions

I was searching for a similar solution and found the solution via: casting raw strings python

Solution 2:

Raw strings are not a different kind of string. They are a different way of describing a string in your source code. Once the string is created, it is what it is.