How to Convert Hex to RGB in Python
- Use int(hex_pair, 16) to convert each two-character hex segment to a decimal number.
- One-liner: tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
- Strip the # prefix before parsing with hex_color.lstrip("#")
- PIL/Pillow users: ImageColor.getrgb("#3B82F6") returns the tuple directly.
Table of Contents
In Python, converting a hex color to RGB comes down to slicing the string into three pairs and using int(pair, 16) to convert each from base 16 to a decimal number. The result is a tuple of three integers between 0 and 255.
Python makes this more concise than most languages. The one-liner fits in a single line with a generator expression. If you are working with images in Pillow, there is even a built-in method that skips the manual conversion entirely.
A Basic Python Hex to RGB Function
Clean and readable:
def hex_to_rgb(hex_color):
hex_color = hex_color.lstrip('#')
r = int(hex_color[0:2], 16)
g = int(hex_color[2:4], 16)
b = int(hex_color[4:6], 16)
return (r, g, b)
hex_to_rgb('#3B82F6') # (59, 130, 246)lstrip('#') removes the hash character if present. int(value, 16) converts each two-character substring from hexadecimal to a Python integer.
One-Liner Hex to RGB in Python
For compact scripts or interactive use:
hex_to_rgb = lambda h: tuple(int(h.lstrip('#')[i:i+2], 16) for i in (0, 2, 4))
hex_to_rgb('#FF6400') # (255, 100, 0)The generator iterates over starting positions 0, 2, and 4 — one for each color channel. Each slice takes two characters and converts them with int(..., 16). The tuple() wraps the result into the standard RGB format.
Using Pillow's ImageColor for Direct Conversion
If you have Pillow installed (common in image processing work), use the built-in method:
from PIL import ImageColor
ImageColor.getrgb('#3B82F6') # (59, 130, 246)
ImageColor.getrgb('#3B82F680') # (59, 130, 246, 128) -- with alphaImageColor.getrgb() handles standard hex, shorthand hex, named colors like "red" or "navy", and even rgba hex. It is the most robust option when working in a Pillow-based image pipeline.
Converting RGB Back to Hex in Python
The reverse direction uses Python's built-in format specification:
def rgb_to_hex(r, g, b):
return '#{:02X}{:02X}{:02X}'.format(r, g, b)
rgb_to_hex(59, 130, 246) # '#3B82F6'The format specifier :02X means: two characters wide, zero-padded, uppercase hexadecimal. Swap X for x if you prefer lowercase hex output. The converter above handles both directions for quick lookups.
Need a Quick Conversion?
Paste your hex code above for an instant RGB result — no Python environment needed.
Open Hex to RGB ConverterFrequently Asked Questions
What does int("3B", 16) return in Python?
59. The second argument tells int() to interpret the string as base-16 (hexadecimal). 3B in hex equals 59 in decimal — the red channel of the color #3B82F6.
Does this work with three-character hex shorthand like #F60?
Not without expanding it first. Add a normalization step: if len(hex_color) == 3: hex_color = "".join(c*2 for c in hex_color). Then #F60 becomes #FF6600 before parsing.
How do I convert a hex color to an HSL tuple in Python?
Use colorsys from the standard library: import colorsys, then call colorsys.rgb_to_hls(r/255, g/255, b/255). Note the return order is (H, L, S) rather than (H, S, L) — and values are 0 to 1 rather than degrees and percentages.
Do I need any third-party packages for basic hex to RGB conversion?
No. The basic function uses only built-in Python string methods and int(). Pillow is optional and only needed if you are already working in an image processing context.

