Python Conversion Programs

Convert RGB To Hex In Python

Last updated January 31, 2024 by Jarvis Silva

In this tutorial we will see how to convert RGB to Hex in python, RGB is a color code which stands for red, green and blue and Hex is another color code format which uses hexadecimal values to indicate a color.

Python Code To Convert RGB To Hex 

def rgb_to_hex(rgb):
    return '#%02x%02x%02x' % rgb
print(rgb_to_hex((255, 255, 255)))

The aboe code defines a function called rgb_to_hex that takes an RGB color value as input and returns its corresponding hexadecimal representation. It uses string formatting to convert each RGB component to a two-digit hexadecimal string.

The function is then called with an example RGB color value of (255, 255, 255) (representing white), and the resulting hexadecimal color representation #ffffff is printed.

You can also run this program by using this online python compiler or on your computer, after running the program you will see it successfully converted RGB color code to Hex color code


Want to know how to convert Hex to RGB color code then read this tutorial: Convert Hex To RGB In Python.

As you can see we have successfully converted RGB Color to Hex in python, I hope you found this tutorial helpful and useful.

Here are some more python programs you will find helpful:

I hope you found what you were looking for from this python tutorial, and if you want more python tutorials like this, do join our Telegram channel to get updated.

Thanks for reading, have a nice day 🙂