What is the most elegant method to get the functionality of the r-prefix on string literals at run-time. That is, a string is constructed dynamically
my_re_str = somefunction()
and then, all the backslashes need to be put back in place whenever there is a '\n', '\b' and the like. The reason for this is that the string is used to compile a regular expression in the form of
my_re = re.compile(my_re_str)
where with constant string literals one might write
my_re = re.compile(r"something")
Is there an elegant method to achieve this without manually replacing all backslash-ed characters?