I believe that this does what you are looking for:

h = input('Enter hex: ').lstrip('#')
print('RGB =', tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))

(The above was written for Python 3)

Sample run:

Enter hex: #B4FBB8
RGB = (180, 251, 184)

Writing to a file

To write to a file with handle fhandle while preserving the formatting:

fhandle.write('RGB = {}'.format( tuple(int(h[i:i+2], 16) for i in (0, 2, 4)) ))
Answer from John1024 on Stack Overflow
🌐
30 Seconds of Code
30secondsofcode.org › home › math › convert between hex and rgb
Python - Convert between hex and RGB - 30 seconds of code
August 11, 2024 - Converting a hexadecimal color ... to do is use a list comprehension in combination with int() and list slice notation to get the RGB components from the hexadecimal string, then use tuple() to convert the resulting list to a tuple...
🌐
Educative
educative.io › answers › how-to-convert-hex-to-rgb-and-rgb-to-hex-in-python
How to convert hex to RGB and RGB to hex in Python
We will get 3 values that correspond to RGB values. Let’s see how this can be implemented in the code. ... Line 1: We define the hex_to_rgb() function that accepts the hex color code values.
Discussions

python - Converting an RGB color tuple to a hexidecimal string - Stack Overflow
See prior SO answer stackoverf... python code snippet to do what i believe you are after. ... The term you're looking for is Hex Triplet. en.wikipedia.org/wiki/Hex_color#Hex_triplet ... For a more general question, with much better answers than here: stackoverflow.com/a/57197866/624066 ... Save this answer. ... Show activity on this post. ... Note that it won't check bounds... ... Is the 2-digit limit really necessary though? As long as the RGB values are ... More on stackoverflow.com
🌐 stackoverflow.com
RGB string value to HEX (Python 3.8)
Show us some example input data, and the data you would like out from your example. What are you using the data for? What's the big picture? More on reddit.com
🌐 r/learnpython
14
1
December 12, 2020
Converting hex to rgb
You've made a fundamental error. The wikihow shows you how to convert a 6-digit hex number into a decimal number. The color codes you are working with are NOT 6-digit hex numbers, but 3 2-digit hex numbers. They are just written all smushed together to make it easier to read and write. So you need to modify hexdec() to convert 2-digit hex numbers, and then call it 3 times: print('red',hexdec(user_input[0:2])) print('green',hexdec(user_input[2:4])) print('blue',hexdec(user_input[4:6])) edit: it would be much more dynamic if you could make hexdec() take any number of digits. More on reddit.com
🌐 r/learnpython
17
0
October 15, 2014
Python RGB to HEX code converter

you should look into bitwise operators

r, g, b = 136, 67, 78

hex_value = (r << 16) | (g << 8) | b

print(f"#{hex_value:x}")

Will make your logic a lot simpler, as RGB/hex works on the principle of bitwise operations in terms of how it works.

More on reddit.com
🌐 r/Python
12
9
November 6, 2022
🌐
AskPython
askpython.com › python › examples › convert-hex-to-rgb
Convert Hex to RGB Values in Python - Simple Methods - AskPython
February 27, 2023 - Let us now feed in a hexadecimal value and run it through our customized function. hex = 'D70A51' print('RGB value for ',hex,' is',Hex_RGB(hex)) ... Now that we have reached the end of this article, hope it has elaborated on the conversion of ...
🌐
Delft Stack
delftstack.com › home › howto › python › python hex to rgb
How to Convert HEX to RGB in Python | Delft Stack
February 12, 2024 - There are two main methods that you can use to convert a Hexadecimal value to an RGB value in Python: the Python Image Library PIL and the self-defined method.
🌐
Pythondex
pythondex.com › convert-hex-to-rgb-python
Convert Hex To RGB In Python - Pythondex
January 31, 2024 - For each segment, int(hex[i:i+2], 16) converts the segment to an integer using base 16 (hexadecimal). The resulting RGB values are collected as a tuple. Finally, the RGB value is displayed using the print() function.
🌐
w3resource
w3resource.com › python-exercises › string › python-data-type-string-exercise-94.php
Python: Convert a hexadecimal color code to its RGB components - w3resource
June 12, 2025 - Use a list comprehension in combination with int() and list slice notation to get the RGB components from the hexadecimal string.
Find elsewhere
Forked by 2 users
Languages: Python
🌐
LabEx
labex.io › tutorials › hex-to-rgb-conversion-13654
Hex to RGB Conversion | LabEx
Use a list comprehension in combination with int() and list slice notation to get the RGB components from the hexadecimal string.
🌐
GitHub
gist.github.com › matthewkremer › 3295567
Python Hex Code to RGB Value · GitHub
November 12, 2018 - def hex_to_rgb(hex): hex = hex.lstrip('#') hlen = len(hex) return tuple(int(hex[i:i + hlen // 3], 16) for i in range(0, hlen, hlen // 3))
🌐
precision
willwang6.wixsite.com › precision › post › 2018 › 02 › 25 › learn-python-through-hex-to-rgb-conversion
Learn python through Hex-to-RGB conversion
February 25, 2018 - On the python module we rename the default “a” output to “RGB” and use line 32 to execute the conversion. Now we should be able to see RGB values of a valid hex code. Both this blog and the last one may be a little verbose.
🌐
CodeSpeedy
codespeedy.com › home › convert rgb to hex color code in python
Convert RGB to hex color code in Python - CodeSpeedy
December 7, 2019 - In the above example, we created the function to convert Hex to RGB and passed the string inside it and then converted it to the tuple of integers.
🌐
Aspose
kb.aspose.com › svg › python › convert-hex-to-rgb-in-python
Convert HEX to RGB in Python
November 27, 2025 - This straightforward code sample shows how to convert color code HEX to RGB in Python. You only need to pass the HEX value to the FromString method and then perform the to_rgb_string operation.
🌐
Wildandfreetools
wildandfreetools.com › home › blog › hex to rgb in python
Hex to RGB in Python: Convert Color Codes with Code Examples | WildandFree Tools
March 17, 2026 - 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.
🌐
GitHub
github.com › Chalarangelo › 30-seconds-of-code › blob › master › content › snippets › python › s › hex-to-rgb.md
30-seconds-of-code/content/snippets/python/s/hex-to-rgb.md at master · Chalarangelo/30-seconds-of-code
Converting a hexadecimal color ... to do is use a list comprehension in combination with int() and list slice notation to get the RGB components from the hexadecimal string, then use tuple() to convert the resulting list to a tuple...
🌐
Reddit
reddit.com › r/pythonlearning › how to convert hex to rgb using python
r/PythonLearning on Reddit: How to convert HEX to RGB using Python
December 25, 2024 - Everything about learning the programming language Python. Members Online • · rao_vishvajit · Share · Share · Be the first to comment · Nobody's responded to this post yet. Add your thoughts and get the conversation going. How to convert HEX to RGB using Python ·
🌐
Reddit
reddit.com › r/pythonprojects2 › how to convert hex to rgb using python
r/PythonProjects2 on Reddit: How to convert HEX to RGB using Python
December 25, 2024 - 42K subscribers in the PythonProjects2 community. A place for people who are learning the programming language 'Python' to come and apply their new…
🌐
YouTube
youtube.com › matador software
Simply Convert RGB and Hex Colour Values using Python - YouTube
In this short tutorial, I'll give insight into why you may need to convert RGB & Hex Colour Values as well as two simple scripts you help you action this wit...
Published: May 16, 2022
Views: 2K