-1

I am trying to print unicode characters for strings. For example, '{' should print as '007B' (with the leading zeroes)

All I can do at the moment is this:

binascii.hexlify(unicode("{")) 
'7b'

I want to be able to give it a string like "test" and it should print "0074006500730074"

2 Answers2

1

Use string formatting:

>>> string = 'test'
>>> ''.join(["{:04x}".format(ord(c)) for c in string])
'0074006500730074'
>>>
juanpa.arrivillaga
  • 77,035
  • 9
  • 115
  • 152
0

Try this:

character = ...

uni_string = hex(ord(character))[2:].zfill(4) 

This gets the unicode number, converts to hex, lops off first two characters, then pads the string with zeros up to length 4

Jerfov2
  • 4,794
  • 3
  • 28
  • 47