drs
drs

Reputation: 5797

How can I print the decimal representation of a unicode string?

I am trying to compare unicode strings in Python. Since a lot of the symbols look similar and some may contain non-printable characters, I am having trouble debugging where my comparisons are failing. Is there a way to take a string of unicode characters and print their unicode codes? i.e.:

>>> unicode_print('❄')
'\u2744'

Upvotes: 1

Views: 195

Answers (1)

utdemir
utdemir

Reputation: 27216

You can encode that string with some other encoding:

>>> s = '❄'
>>> s.encode() # "utf8" by default
b'\xe2\x9d\x84'

And for the output you specified, I just found this from here:

>>> s.encode("unicode_escape")
b'\\u2744'

Upvotes: 1

Related Questions