Charlie Fan
Charlie Fan

Reputation: 9

How to get the unicode of a character in Python?

Wanna get the unicode of chinese or vietnamese's han-nom and japanese characters I've tried these code

text = "𬖰";

br = text.encode("unicode-escape");

print(br);

and got

b'\\U0002c5b0'

But what should I do when I want to have something like U+2C5B0 or U2C5B0 ?

Upvotes: 0

Views: 55

Answers (1)

blhsing
blhsing

Reputation: 107015

You can use the ord function to get the character's numeric code point and format it with the 04X specifier in an f-string to display the code point as uppercased hexadecimals that are 0-padded up to 4 characters wide:

print(f'U+{ord(text):04X}')

Demo here

Upvotes: 2

Related Questions