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