2

lets say i have a string,
"Hello–World"

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

1 Answers1

2

Use str.encode with unicode_escape:

>>> print(s.encode('unicode_escape'))
b'Hello\\u2013World'

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

>>> print(s.encode('unicode_escape').decode())
Hello\u2013World
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • thanks, didn't know that encoding method exists – Faded Jayden Nov 04 '21 at 15:30
  • More encodings here: https://docs.python.org/3/library/codecs.html#text-encodings – Corralien Nov 04 '21 at 15:32
  • I think it would be better to delete this answer, because it's a duplicate of https://stackoverflow.com/a/32280815 and fixes would have to be made to both. If you don't delete it, it should mention that this encoding also produces other Python escape sequences like `\t` and `\xXX`. – benrg Nov 04 '21 at 17:08