hex(): integer number to its hexadecimal representation

number :input integer number
Return the hexal number as string.


print(hex(my_num))  # 0x1b
my_num=-27 # Negative integer
print(hex(my_num))  # -0x1b

Using Float

This will generate TypeError
TypeError: 'float' object cannot be interpreted as an integer

Binary to hex

print(hex(0b11011)) # 0x1b
In above code the input binary number is equivalent to integer 27.

You can use bin() to get the binary output
print("Binary Number : ", bin(27)) # Binary Number :  0b11011

octal to hex

print(hex(0o33)) # 0x1b
In above code the input Octal number is equivalent to integer 27 You can use oct() to get the Octal output
print("Octal  Number : ", oct(27)) # Octal  Number :  0o33
In our hex output, 0x is always prefixed. As the output is a string we can remove it like this
print(my_hex[2:]) # 1b
Without using hex() and by using format()
print(format(my_num,'x')) # 1b

Hex code for Colors

Hexadecimal (hex) color codes are a way to represent colors in a format that uses a combination of letters and numbers. Each color is represented by a six-digit code that consists of three pairs of two characters each. These pairs represent the intensity of red, green, and blue components of the color. Here's how the hex code works:
  1. The first two characters represent the red component.
  2. The next two characters represent the green component.
  3. The last two characters represent the blue component.
For example:
  • The hex code #FF0000 represents pure red (maximum red, no green, no blue).
  • The hex code #00FF00 represents pure green (no red, maximum green, no blue).
  • The hex code #0000FF represents pure blue (no red, no green, maximum blue).
From HEX code to RGB ( Red Green Blue ) values.
The commented part can be used to get the return value as Tuple for further processing.
def hex_to_rgb(hex):
  #rgb = []
  for i in (0, 2, 4):
    #decimal = int(hex[i:i+2], 16)
  #return tuple(rgb)
  return str1
print(hex_to_rgb('#FF65AA'.lstrip('#'))) # Output RGB(255,101,170)

Key Points About hex() Function

  • Output Format: The result is a string that represents the hexadecimal value, prefixed with "0x".
  • Negative Integers: The hex() function also works with negative integers, returning the hexadecimal representation of the two's complement of the given number.
  • Non-Integer Inputs: If the input to hex() is not an integer, a TypeError will be raised, emphasizing the need for integer inputs.
In summary, the hex() function provides a straightforward way to convert integers to their hexadecimal representation in Python, making it a useful tool for a variety of programming tasks that require hexadecimal values.
Check the Colour list here for Colour names and HEX values
All Built in Functions in Python
max() Bitwise operators int() oct() float()

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer