Python3 – replacing non ascii characters to their unicode representative value?

lets say i have a string,

how would I convert it to something like this
where "\u2013" is the unicode representative of "–"

>Solution :

Use str.encode with unicode_escape:

>>> print(s.encode('unicode_escape'))

If you want a string (and to a byte string like above):

>>> print(s.encode('unicode_escape').decode())

Leave a Reply