The hex() function converts a specified integer number into a hexadecimal string representation.

Use int(x, base) with 16 as base to convert the string x to an integer. Call hex(number) with the integer as number to convert it to hexadecimal.

hex_string = "0xAA"

"0x" also required

an_integer = int(hex_string, 16)

hex_value = hex(an_integer)

print(hex_value)

Output

0xaa

Answer from Thuấn Đào Minh on Stack Overflow
Discussions

converters - convert string to hex in python - Stack Overflow
I have a script that calls a function that takes a hexadecimal number for an argument. The argument needs to the 0x prefix. The data source is a database table and is stored as a string, so it is More on stackoverflow.com
🌐 stackoverflow.com
Converting string to hex in Python 3.5
I am having trouble converting a string of a 4-byte hex value into a hex value and appending it to a string. For example, I have a string "afbfcfdf"… More on reddit.com
🌐 r/learnpython
18
1
September 5, 2016
How to convert character to hexadecimal and back?
I tried to code to convert string to hexdecimal and back for controle as follows: input = 'Україна' table = [] for item in input: table.append(item.encode('utf-8').hex()) output = '' for item in table: output += chr(int(item, 16)) print(output) To my surprise the output is total different ... More on discuss.python.org
🌐 discuss.python.org
6
0
March 22, 2023
python - How to convert a hex string to hex number - Stack Overflow
I want to convert a hex string (ex: 0xAD4) to hex number, then to add 0x200 to that number and again want to print that number in form of 0x as a string. i tried for the first step: str(int(str(i... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python how-to's" › "how to convert string to hex in python"
How to Convert String to Hex in Python | Delft Stack
February 2, 2024 - In this article, we’ll discuss the various ways to convert a string to hexadecimal in Python. In Python, the encode() method is a string method used to convert a string into a sequence of bytes. It takes an encoding scheme as an argument, such as utf-8, utf-16, ascii, etc., and returns a bytes object containing the encoded representation of the original string.
🌐
Linux Hint
linuxhint.com › string-to-hexadecimal-in-python
String to Hexadecimal in Python – Linux Hint
And then convert those strings to bytes using the encode () method. And then, we print those bytes, which we can see in the output line number 1. Line 10 to 13: We call the hex () method using the dot operator, and now the bytes are converted to the hexadecimal string value we required.
🌐
CoenRaets
jsonviewer.ai › python-string-to-hex
Python String to Hex [Explained with Code Examples] - JSON Viewer
July 5, 2023 - From the example code defines a ... be used for various purposes like data transmission or storage. The hex() method cannot create a hexadecimal representation of a string without first converting it to bytes....
🌐
Python Guides
pythonguides.com › convert-string-to-hexadecimal-in-python
How to Convert String to Hex in Python?
April 3, 2025 - The most simple approach to convert a string to hex in Python is to use the string’s encode() method to get a bytes representation, then apply the hex() method to convert those bytes to a hexadecimal string.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › converting string to hex in python 3.5
r/learnpython on Reddit: Converting string to hex in Python 3.5
September 5, 2016 - By default, the third parameter is 'strict', which means an exception is raised if the bytes in question are invalid for the specified encoding. 'replace' means that the Unicode replacement character (code point U+FFFD) is used for bytes that couldn't be interpreted and had to be skipped; 'ignore' means they're just skipped with no representation in the output string.
🌐
Python Forum
python-forum.io › thread-1715.html
how to convert a string to hex
January 22, 2017 - what is the best way to convert a string to hexadecimal? the purpose is to get the character codes to see what is being read in from a file. i already have command line tools that display the file in hexadecimal. i want to match that up with what...
🌐
Iditect
iditect.com › faq › python › python-string-to-hex.html
Python string to hex
To convert a Python string to its hexadecimal representation, you can use the bytes object and its hex() method.
🌐
GeeksforGeeks
geeksforgeeks.org › python-hex-function
hex() function in Python - GeeksforGeeks
May 9, 2025 - Returns: A string representing the hexadecimal form of the integer. The string is prefixed with 0x, indicating that it is in base-16. Example 1: In this example, we convert an ASCII character and a floating-point number to their corresponding hexadecimal values.
🌐
Python.org
discuss.python.org › python help
How to convert character to hexadecimal and back? - Python Help - Discussions on Python.org
March 22, 2023 - I tried to code to convert string ... follows: input = 'Україна' table = [] for item in input: table.append(item.encode('utf-8').hex()) output = '' for item in table: output += chr(int(item, 16)) print(output) To my surprise ...
🌐
ActiveState
code.activestate.com › recipes › 496969-convert-string-to-hex
Convert string to hex « Python recipes « ActiveState Code
toHex = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x]) The builtin string-method "join" joins every element of the list to the string by re-using the string.
🌐
Sling Academy
slingacademy.com › article › python-ways-to-convert-a-string-to-a-hexadecimal-value
Python: 3 ways to convert a string to a hexadecimal value - Sling Academy
May 27, 2023 - This approach works with both old ... to bytes using the desired encoding, such as encode("utf-8"). Call the binascii.hexlify() function to convert the bytes to a hexadecimal representation....
🌐
Finxter
blog.finxter.com › home › learn python blog › python print string as hex: converting text to hexadecimal format
Python Print String as Hex: Converting Text to Hexadecimal Format - Be on the Right Side of Change
January 16, 2024 - When you’re looking to print a string as hex in Python, you’ll discover that the language’s flexibility allows for easy conversion. For instance, you can utilize built-in functions and methods that Python offers to encode a string into bytes and then represent it in hexadecimal format.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-hex
Python hex() | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
CodeRivers
coderivers.org › blog › python-string-to-hex
Python String to Hexadecimal Conversion: A Comprehensive Guide - CodeRivers
February 22, 2026 - In this example: 1. First, the string "Hello" is encoded to a byte string using the UTF - 8 encoding. 2. Then, the hexlify function from the binascii module is used to convert the byte string to a hexadecimal representation.
🌐
CodeRivers
coderivers.org › blog › string-to-hex-python
Converting Strings to Hexadecimal in Python - CodeRivers
February 22, 2026 - For example, hashing functions often produce hexadecimal output, which can be more secure and easier to manage than the original string. - Data Representation: Sometimes, it's more convenient to store or display data in hexadecimal format. For instance, when debugging, hexadecimal can provide a more detailed view of the underlying data. The binascii module in Python provides functions for converting between binary and various ASCII-encoded representations, including hexadecimal.