number :input integer number
Return the hexal number as string.
print(hex(my_num)) # 0x1b
This will generate TypeError
TypeError: 'float' object cannot be interpreted as an integer
Binary to hex
In above code the input binary number is equivalent to integer 27.
print(hex(0b11011)) # 0x1b
You can use bin() to get the binary output
print("Binary Number : ", bin(27)) # Binary Number : 0b11011
octal to hex
In above code the input Octal number is equivalent to integer 27
You can use oct() to get the Octal output
print(hex(0o33)) # 0x1b
In our hex output,
print("Octal Number : ", oct(27)) # Octal Number : 0o33
0x is always prefixed. As the output is a string we can remove it like this
Without using hex() and by using format()
print(my_hex[2:]) # 1b
«All Built in Functions in Python
print(format(my_num,'x')) # 1b
Bitwise operators »
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.