In this tutorial, I will explain how to convert hexadecimal string to integer in Python. As a developer working on a project for one of my clients, I recently faced an issue where I needed to convert hexadecimal strings to integers. After researching and experimenting with various methods, I discovered several effective ways to accomplish this task. I will share my findings and provide detailed examples.
Convert Hexadecimal String to Integer in Python
Before getting into the conversion process, let’s briefly discuss what hexadecimal numbers are. Hexadecimal (hex) is a base-16 number system that uses digits 0-9 and letters A-F to represent values. Each hex digit represents a 4-bit binary value. For example, the hex string “FF” is equivalent to the decimal value 255.
Read How to Remove Characters from a String in Python?
Method 1. Use int() Method
The most simple way to convert a hexadecimal string to an integer in Python is by using the built-in int() function. The int() function takes two arguments: the hex string and the base (16 for hexadecimal). Here’s an example:
hex_string = "1A"
integer_value = int(hex_string, 16)
print(integer_value) Output:
26I have executed the above example code and added the screenshot below.

In this example, the hex string “1A” is converted to the integer value 26.
Check out How to Remove HTML Tags from a String in Python?
Handle Hex Strings with ‘0x’ Prefix
Sometimes, hex strings may include a “0x” prefix to indicate that they are in hexadecimal format. To convert such strings, you can simply remove the prefix using string slicing:
hex_string = "0x1A"
integer_value = int(hex_string[2:], 16)
print(integer_value) Output:
26I have executed the above example code and added the screenshot below.

By slicing the string from index 2 onwards, we remove the “0x” prefix before passing it to the int() function.
Read How to Convert a Comma-Separated String to a List in Python?
Method 2. Use int() and translate()
Another approach to convert hex strings to integers is by using the int() function in combination with the translate() method. This method is useful when you have a hex string that may contain whitespace or other characters. Here’s an example:
hex_string = "1A " # Contains spaces
integer_value = int(hex_string.replace(" ", ""), 16) # Remove spaces and convert
print(integer_value)Output:
26I have executed the above example code and added the screenshot below.

In this case, the translate() method removes any whitespace characters from the hex string before passing it to the int() function.
Check out How to Split String by Whitespace in Python?
Handle Exceptions
When working with user input or external data sources, it’s important to handle potential exceptions that may occur during the conversion process. The most common exception is the ValueError, which is raised when the hex string contains invalid characters. Here’s an example of how to handle exceptions:
try:
hex_string = "1A"
integer_value = int(hex_string, 16)
print(integer_value) # Output: 26
except ValueError:
print("Invalid hexadecimal string.")By using a try-except block, you can gracefully handle any ValueErrors that may occur and provide appropriate feedback to the user.
Check out How to Repeat a String Multiple Times in Python?
Conclusion
In this tutorial, I have explained how to convert hexadecimal string to integer in Python. I discussed mainly two methods such as using the int() method and int() and translate() method. I also discussed how to handle exceptions.
You may also read:
- How to Convert Datetime to String in Python?
- How to Concatenate String and Int in Python?
- How to Remove Punctuation from Strings in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.