Reverse repr function in Python [duplicate]

I think what you’re looking for is ast.literal_eval:

>>> s = repr("ab\r")
>>> s
"'ab\\r'"
>>> from ast import literal_eval
>>> literal_eval(s)
'ab\r'

Leave a Comment