-2

As an example, how would I get the code for § in python. § is be represented as '\u00a7', but I would like it to give me the code 00a7, so I can use it in the creation of code files. Also I would like this to work if I just put the letter 'a'

sourrabbit
  • 126
  • 6

2 Answers2

0

You can encode and decode stings with

>>> '§'.encode()
b'\xc2\xa7'
>>> b'\xc2\xa7'.decode()
'§'

That is one way of accessing byte codes.

Max
  • 145
  • 2
  • 15
0

Something along the lines of this?

c = '\\u' + hex(ord(u'§'))[2:].rjust(4, '0')
print(c)  # Prints \u00a7
jmd_dk
  • 12,125
  • 9
  • 63
  • 94