HOWTO · Python

How to Convert String to Hex in Python

This tutorial shows how to convert a Python string to hexadecimal.

On this page

A Python string is text; hexadecimal output can represent either the bytes produced by an encoding or each character’s Unicode code point. For a portable byte representation, choose an encoding, call encode(), and convert those bytes with bytes.hex(). UTF-8 is a common choice when the result must preserve non-ASCII text across systems.

The example below compares UTF-8 byte hex with code-point hex for the same text. For this input, it prints compact UTF-8 byte hex, space-grouped byte hex, and then the unpadded code-point hex.

Convert a Python String to Hexadecimal with encode() and hex()

str.encode() converts text to bytes using the named encoding. Then the bytes object’s hex() method returns two hexadecimal digits for each byte. The encoding matters: for example, é is encoded as two bytes in UTF-8, so its byte hex is c3a9, while its code point is e9.

Run this example:

text = "Café"
encoded = text.encode("utf-8")
print(encoded.hex())
print(encoded.hex(" "))
print("".join(hex(ord(character))[2:] for character in text))
436166c3a9
43 61 66 c3 a9
436166e9

The first line is compact UTF-8 byte hex; the second groups each byte with a space for readability. The third line shows code points and therefore has different semantics. Use the ungrouped form when you need a conventional continuous hex string; separators are useful for inspection but are part of the returned string.

Use binascii.hexlify() as an Alternative

The standard-library binascii.hexlify() function also converts bytes to hexadecimal bytes. Encode the string first, then decode the result if your next step expects a Python text string:

import binascii

text = "Café"
hex_text = binascii.hexlify(text.encode("utf-8")).decode("ascii")

The value of hex_text is 436166c3a9.

The returned value before decoding is a bytes object containing ASCII characters such as b'436166c3a9'. Decoding it with ASCII is appropriate because the hex digits are ASCII, regardless of the original text’s encoding. This method is useful when an API specifically expects binascii or bytes output; for ordinary text output, bytes.hex() is shorter.

Convert Character Code Points with ord() and hex()

To represent each character’s Unicode code point instead of its encoded bytes, apply ord() to each character and pass that integer to hex(). Remove the 0x prefix if you want just the digits. This is a different representation from UTF-8: the code point for é is e9, while its UTF-8 bytes are c3 a9.

If you concatenate code-point digits, variable-width values can make the boundaries unclear. For example, code points 0x41 and 0x1f600 become 411f600 without separators, which does not show where one value ends and the next begins. Keep delimiters or use fixed-width padding when another program must parse the individual code points.

Choose Between Encoded Bytes and Character Code Points

Choose text.encode(encoding).hex() when you need the bytes that will be stored, transmitted, or compared under a specific encoding. Choose the ord() approach only when you specifically need Unicode code points; it does not produce the encoded byte sequence.

Python’s built-in hex() accepts an integer, not a string. Applying it directly to text raises TypeError. Converting a numeric string that already contains hexadecimal digits with int(value, 16) is a separate parsing task, not a way to encode arbitrary text. For regular string-to-hex conversion, specify the encoding and use encode() followed by hex().