Quick answer: Convert hexadecimal text to a decimal integer with int(text, 16). Decide whether the input may contain a 0x prefix, whitespace, signs, or labels, then validate that format at the boundary and catch ValueError instead of silently accepting malformed data.

To convert hexadecimal to decimal in Python, pass the text to int() with base 16. Hexadecimal is base 16, so it uses digits 0 through 9 and letters a through f.
The official Python documentation covers int() and hex().
The most important rule is to pass the correct base. A string such as "ff" cannot be parsed as base 10, but it is valid base 16 and equals 255 in decimal.
Hex values often appear in color codes, memory addresses, binary protocols, logs, file formats, and low-level debugging output. Python can parse them cleanly as long as the text is in a supported shape.
Before parsing, decide which shapes your program accepts. Some sources use plain text such as ff, while others include prefixes such as 0xff. Some data may be uppercase, padded with whitespace, or mixed with labels. A clear parser should accept only the formats you intend to support.
Decimal output from int() is a normal Python integer. After conversion, you can compare it, store it, use it in arithmetic, or format it for display just like any other integer.
Convert A Hex String With int
Use int(text, 16) for plain hexadecimal text.
hex_text = "ff"
decimal = int(hex_text, 16)
print(decimal)
The result is the decimal integer 255.
Letters can be uppercase or lowercase. Python accepts "ff" and "FF" for the same value.
This is the best method when the source consistently provides base-16 text without prefixes. It is short, explicit, and easy to test.
Convert Text With A 0x Prefix
Python can parse the common 0x prefix when the base is correct.
hex_text = "0x2a"
decimal = int(hex_text, 16)
print(decimal)
This prints 42.
The prefix is a useful visual signal that the number is written in base 16. Keep it when the source data includes it consistently.
You do not need to remove the prefix when passing base 16; Python understands it. Removing it manually is only useful when you want one helper to handle both prefixed and unprefixed text in the same way.

Infer The Base From A Prefix
Pass base 0 when the prefix should decide the base.
samples = ["0xff", "0b1010", "0o77", "42"]
for text in samples:
print(text, int(text, 0))
With base 0, Python recognizes prefixes such as 0x, 0b, and 0o.
Use this only when prefixed input is expected. If plain hexadecimal text such as "ff" is allowed, pass base 16 instead.
Base inference is convenient for mixed prefixed data, but it can make validation rules less obvious. Prefer a fixed base when your input format is known.
Clean Whitespace Before Conversion
Use strip() when input may include leading or trailing whitespace.
hex_text = " 7f\n"
clean = hex_text.strip()
decimal = int(clean, 16)
print(decimal)
strip() removes surrounding whitespace but does not change the base or remove invalid characters.
Do not remove arbitrary letters or symbols unless your parser clearly supports that format. A strict parser is easier to debug than one that silently changes the input.
For example, a color value such as #ffcc00 needs a parser that deliberately handles the leading #. Do not strip every non-digit character just to make conversion pass.

Convert A List Of Hex Strings
A list comprehension converts several hexadecimal strings at once.
hex_values = ["0a", "10", "2f", "ff"]
decimals = [int(item, 16) for item in hex_values]
print(decimals)
This is useful for parsing rows, protocol fields, or extracted text values.
If one item may be invalid, use a helper function with error handling instead of letting the whole loop fail without context.
For batch data, store the original text beside the converted integer while debugging. That makes it easy to find the exact input row that produced a parsing error.
Validate Hex Text Safely
A helper can normalize the input, check allowed characters, and return None for unsupported text.
def hex_to_decimal(text):
clean = text.strip().lower()
if clean.startswith("0x"):
clean = clean[2:]
if not clean:
return None
allowed = set("0123456789abcdef")
if any(char not in allowed for char in clean):
return None
return int(clean, 16)
for text in ["ff", "0x2a", "12g", ""]:
print(text, hex_to_decimal(text))
This helper accepts plain hex text and 0x-prefixed text, but rejects empty or unsupported input.
In production code, decide whether invalid input should return None, raise an exception, or produce a validation message. The right answer depends on whether the parser is internal, batch-oriented, or user-facing.
Returning None can be helpful for form-style validation. Raising an exception is often better for internal code where invalid data means a caller used the function incorrectly.
When data comes from bytes, decode it to text first, then parse the text as hexadecimal. Keep decoding separate from numeric conversion so errors point to the correct stage.
In short, use int(hex_text, 16) for hexadecimal-to-decimal conversion. Clean harmless whitespace, handle prefixes intentionally, and validate text before parsing when the input comes from files, forms, or external systems.
Parse Plain Hexadecimal
int(‘ff’, 16) returns the integer 255. Letters can be uppercase or lowercase, and the returned value is an ordinary Python integer that can be compared, stored, or used in arithmetic.

Handle Prefixes Deliberately
If the input may contain 0x or another supported base prefix, int(text, 0) can infer the base. If the format is strictly hexadecimal, normalize the prefix and use base 16 so the contract remains obvious.
Normalize Input Safely
Strip permitted surrounding whitespace and decide whether a leading sign is valid. Do not remove arbitrary characters or labels with a broad replacement because that can turn a malformed identifier into a different number.

Convert Collections
For a list of strings, apply the same parser to every item and report which element failed. Keep the input and output collections aligned when the values represent colors, addresses, protocol fields, or other records.
Format The Result Back
Use hex(value) or format(value, ‘x’) when the decimal integer needs to be displayed as hexadecimal again. Document casing and prefix requirements if the output is consumed by another system.
Python’s int documentation defines base-aware string conversion and errors. Related references include bytes and numeric data, dynamic fields, and input tests.
For related conversion workflows, compare bytes and numeric data, dynamic fields, and input tests when parsing hexadecimal values.
Frequently Asked Questions
How do I convert hex to decimal in Python?
Call int(hex_text, 16) and store the returned integer.
Can int parse a 0x prefix?
Yes, pass base 0 to let Python infer a supported prefix, or normalize the input and use base 16 when the format is specifically hexadecimal.
Does Python accept uppercase hexadecimal letters?
Yes. Hexadecimal digits A through F and a through f represent the same values.
How should invalid hex input be handled?
Catch ValueError at the input boundary, report the accepted format, and avoid silently treating malformed text as a different number.