You could use ord()
to get the integer codepoint for each character and format that instead:
''.join(format(ord(c), '04x') for c in u_str)
Demo:
>>> u_str = u'12你好'
>>> ''.join(format(ord(c), '04x') for c in u_str)
'003100324f60597d'
or you could encode to UTF-16 (big endian) and use binascii.hexlify()
on the result; this is probably the faster option:
from binascii import hexlify
hexlify(u_str.encode('utf-16-be'))
Demo:
>>> from binascii import hexlify
>>> hexlify(u_str.encode('utf-16-be'))
'003100324f60597d'
The latter also handles characters outside of the BMP, requiring 4 bytes per codepoint, which would be encoded using UTF-16 surrogate pairs:
>>> hexlify(u'\U0001F493'.encode('utf-16-be'))
'd83ddc93'