-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
  • 88,713
  • 10
  • 131
  • 172
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
  • 5,264
  • 5
  • 30
  • 52