- 1Python aiter() Function – Get Async Iterator in Python
- 2Python all() Function – Check If All Elements Are True
- 3Python anext() Function – Get Next Item from Async Iterator
- 4Python any() Function – Check if Any Element is True
- 5Python ascii() Function – Get ASCII Representation of Objects
- 6Python bin() Function – Convert Integer to Binary String
- 7Python bool() Function – Convert to Boolean
- 8Python breakpoint() Function – Add Debug Points in Your Code
- 9Python bytearray() Function – Create and Modify Binary Data
- 10Python bytes() Function – Create Immutable Byte Objects
- 11Python callable() Function – Check If Object Is Callable
- 12Python chr() Function – Get Character from Unicode Code Point
- 13Python classmethod() Function – Convert Method to Class Method
- 14Python compile() Function – Compile Source Code at Runtime
- 15Python complex() Function – Create Complex Numbers Easily
- 16Python delattr() Function – Delete Attribute from Object
- 17Python dict() Function – Create and Use Dictionaries Easily
- 18Python dir() Function – List Attributes of Objects
- 19Python divmod() Function – Quotient and Remainder in One Go
- 20Python enumerate() Function – Loop with Index Easily
- 21Python eval() Function – Evaluate Expressions from Strings
- 22Python exec() Function – Execute Dynamic Python Code
- 23Python filter() Function – Filter Items in a List
- 24Python float() Function – Convert to Floating Point Number
- 25Python format() Function – Format Strings Easily
- 26Python frozenset() Function – Create Immutable Sets
- 27Python getattr() Function – Access Object Attributes Dynamically
- 28Python globals() Function – Access Global Symbol Table
- 29Python hasattr() Function – Check If an Object Has an Attribute
- 30Python hash() Function – Generate Hash Values of Objects
- 31Python help() Function – Get Help on Python Functions, Classes, and Modules
- 32Python hex() Function – Convert Integer to Hexadecimal
- 33Python id() Function – Get Memory Address of an Object
- 34Python input() Function – Get User Input Easily
- 35Python int() Function – Convert to Integer
- 36Python isinstance() Function – Check Type of a Variable
- 37Python issubclass() Function – Check Class Inheritance
- 38Python iter() Function – Create an Iterator from Iterable
- 39Python len() Function – Get the Length of Strings, Lists, and More
- 40Python list() Function – Convert Data to a List
- 41Python locals() Function – Get Local Symbol Table as a Dictionary
- 42Python map() Function – Apply a Function to Each Element
- 43Python max() Function – Find the Largest Value
- 44Python memoryview() Function – Work With Binary Data Efficiently
- 45Python min() Function – Find the Smallest Value
- 46Python next() Function – Retrieve Next Item from an Iterator
- 47Python object() Function – Base Object Constructor Explained
- 48Python oct() Function – Convert Number to Octal
- 49Python open() Function – Read, Write, and Create Files Easily
- 50Python ord() Function – Get Unicode Code of a Character
- 51Python pow() Function – Raise a Number to a Power
- 52Python print() Function – Print Output to the Console
- 53Python property() Function – Create Managed Attributes
- 54Python range() Function – Generate Number Sequences Easily
- 55Python repr() Function – Get String Representation of an Object
- 56Python reversed() Function – Reverse Iterables Easily
- 57Python round() Function – Round Numbers to Nearest Integer or Decimal Places
- 58Python set() Function – Create a Set from Iterable
- 59Python setattr() Function – Dynamically Set Object Attributes
- 60Python slice() Function – Create Slice Objects for Lists and Strings
- 61Python sorted() Function – Sort Any Iterable Easily
- 62Python staticmethod() Function – Create Static Methods in Classes
- 63Python str() Function – Convert to String Easily
- 64Python sum() Function – Add Items of an Iterable
- 65Python super() Function – Call Parent Class Methods
- 66Python tuple() Function – Create a Tuple from Iterable
- 67Python type() Function – Get the Type of an Object
- 68Python vars() Function – View Object’s Attributes
- 69Python zip() Function – Combine Iterables Element-Wise
- 70Python __import__() Function – Dynamic Module Import
- 71Python str.format() – Format Strings Easily with Placeholders
- 72Python String format_map() – Format Strings with a Mapping
Python hex() Function – Convert Integer to Hexadecimal
Python hex()
Function
The hex() function in Python is used to convert an integer number to a hexadecimal string (base 16), prefixed with 0x
.
This is useful when working with colors, memory addresses, or binary data formats.
Syntax
hex(number)
Parameters:
number
– A valid integer (positive or negative)
Returns:
- A string starting with
'0x'
followed by the hexadecimal equivalent of the integer.
Example: Convert Positive Integer to Hex
print(hex(255))
0xff
Example: Convert Negative Integer to Hex
print(hex(-42))
-0x2a
Use Case: RGB Colors in Web Design
Each color component in an RGB color code is often represented in hexadecimal:
r = 255
g = 165
b = 0
hex_color = f"#{hex(r)[2:]}{hex(g)[2:]}{hex(b)[2:]}"
print(hex_color)
#ffa500
Note: [2:]
is used to remove the '0x'
prefix.
Common Mistakes
- Passing a float:
hex(3.14)
will raiseTypeError
- Passing a string:
hex("10")
is invalid - Always pass an integer type: use
int()
if needed
Interview Tip
In interviews, hex()
is often used in bit manipulation or encoding problems. Know how to combine it with int()
, bin()
, or ord()
.
Summary
hex()
converts an integer to a base-16 (hexadecimal) string- Output starts with
'0x'
for positive or'-0x'
for negative - Does not work on float or string types directly
Practice Problem
Write a program that reads an integer from the user and prints its hexadecimal equivalent.
num = int(input("Enter an integer: "))
print("Hexadecimal:", hex(num))