Ways of converting numbers to and from text in python

There are several methods of converting numbers to and from strings in python.

converting strings to numbers

nr = int(txt, base)

converting numbers to strings

using string formatting:

txt = "%#03d" % nr
txt = "%#03x" % nr
txt = "%#03o" % nr

txt = "{0:#03b}".format(nr)
txt = "{0:#03o}".format(nr)
txt = "{0:#03x}".format(nr)
txt = "{0:#03d}".format(nr)

using specialized functions:

txt = bin(nr)
txt = hex(nr)
txt = oct(nr)

using python3 integer from_bytes and to_bytes methods:

nr = int.from_bytes(a2b_hex(txt), "little")
txt = b2a_hex(nr.to_bytes(16, "little"))

somewhat related:

nr.bit_length()