In a single line: Given a string, how can we get to a raw string representation of it?
I am generating a file, in which I have regexes, like '[ \\n\\t\\r\\f\\v]'
which I wish to render as raw strings. How can this be achieved?
P.S: I actually intend on representing strings with double quotes too, so a string '\''
is rendered as "'"
. I need help with that too..
By "raw string" I mean the type of strings we use often for regexps:
>>> r"[ \r\n\f\v\t]"
'[ \\r\\n\\f\\v\\t]'
# assuming to_raw is the function
>>> print to_raw(r"[ \r\n\f\v\t]")
r'[ \r\n\f\v\t]'
>>> print to_raw("\\\\")
r'\\'
>>> print to_raw("'")
r"'"