@paul23 You want
'hellö'.encode('ascii', 'backslashreplace').decode("unicode_escape")
, but there are caveats. See
stackoverflow.com/questions/4020539. The reason your approach doesn't work is that
'backslashreplace'
is an
error-handling protocol, not an encoding. When you encode, the resulting
bytes
contains bytes corresponding to backslashes,
which are perfectly valid UTF-8, so no error handling happens on the round trip.