Python: 3 ways to convert a string to a hexadecimal value

Updated: May 27, 2023 By: Khue Post a comment

In Python and the world of software development, a hexadecimal value (or hex value) is a representation of a number using base-16 digits and alphabets. It consists of digits 0-9 and letters A-F. The prefix 0x is optional and sometimes used to indicate that a value is hexadecimal, but it is not required. Hexadecimal values are used for memory addresses, bitwise operations, data encoding, and representing colors in computer graphics, etc.

This succinct and straight-to-the-point article will walk you through several different ways to turn a string into a hexadecimal value in Python. Without any further ado, let’s get started!

Using the hex() method

If you are using Python 3.5 or newer, you can use the encode() and the hex() methods to convert a given string to a hex value with a single line of code.


text = "Sling Academy"
hex = text.encode("utf-8").hex()



The syntax is very concise and intuitive. I guess you will like it the most. There is one more thing that I think might help you when using the hex() function. If you want the resulting hex string to be more readable, you can set a custom separator with the sep parameter as shown below:

text = "Sling Academy"
hex = text.encode("utf-8").hex(sep="_")



Using the binascii.hexlify() function

This approach works with both old versions of Python (2.x) and new versions of the programming language (3.x). What we will do are:

  1. Encode the string to bytes using the desired encoding, such as encode("utf-8").
  2. Call the binascii.hexlify() function to convert the bytes to a hexadecimal representation.
  3. Decode the resulting bytes as a string to obtain the hex value.

Code example:

import binascii

string = "Sling Academy"
encoded_bytes = string.encode('utf-8')
hex_value = binascii.hexlify(encoded_bytes).decode('utf-8')




This approach is straightforward, works will all versions of Python and doesn’t rely on any third-party library. However, it requires encoding and decoding steps limited to ASCII characters and might not handle non-ASCII characters well.

Using list comprehension and the ord() function

In this technique, we will convert each character in the string to its corresponding Unicode code point and format it as a hexadecimal value. The steps are:

  1. Iterate through each character in the string.
  2. Use ord() to obtain the Unicode code point of each character.
  3. Format the code point as a hexadecimal string by using the format() function.
  4. Join the hexadecimal strings together with the join() method.

A code example is worth more than a thousand words:

string = "Sling Academy"
hex_value = ''.join([format(ord(char), 'x') for char in string])


Because we use the same input as other examples in this article, the output won’t be a new thing:


The advantage of this technique is that can handle non-ASCII characters and is flexible and customizable. The trade-off is that it requires iterating through each character, which may be less efficient for extremely large strings.

Notify of
Inline Feedbacks
View all comments