The hex codec has been chucked in 3.x. Use binascii instead:

>>> binascii.hexlify(b'hello')
b'68656c6c6f'
Answer from Ignacio Vazquez-Abrams on Stack Overflow
Discussions

Converting a string to Hex in Python 3 - Stack Overflow
How can I convert a string like "AAAA" to "/x41/x41/x41/x41" hex format in python ? There are many functions like binascii.hexlify and all, but those just converts it to 4141414... More on stackoverflow.com
🌐 stackoverflow.com
String to HEX
I've tried .encode, binascii.hexlify, and a few other things. Ok, well, encode was the correct one, so do that. More on reddit.com
🌐 r/learnpython
7
5
September 2, 2024
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
python - How to convert an int to a hex string? - Stack Overflow
For reference on this neat feature, PEP 498 -- Literal String Interpolation 2018-09-22T04:05:55.357Z+00:00 ... IMHO, a bit more "readable" option can be '0x{:X}'.format(15) (notice X is in uppercase). For 2034 it will print 0x7F2 instead of 7f2 2020-03-04T04:53:29.443Z+00:00 ... This answer should be combined with that from user3811639, which shows some variants of the format specifier to give us a particular number of hex digits, which is often a requirement when converting ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
GitHub
gist.github.com › fivepoint-0 › 69a1201dbdfba04b0e44c3d435ebf5f5
Python 3 - Convert String to Hex String · GitHub
Python 3 - Convert String to Hex String. GitHub Gist: instantly share code, notes, and snippets.
🌐
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.
🌐
Stack Overflow
stackoverflow.com › questions › 66783581 › converting-a-string-to-hex-in-python-3
Converting a string to Hex in Python 3 - Stack Overflow
I did not had to include \x in front of every byte. Python unhexlify did it automatically I guess. Input = "AAAA" Input = str.encode(Input) Input = hexlify(Input) Input = unhexlify(Input) #If you print it, it will just print b'AAAA'
🌐
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
🌐
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 versions of Python (2.x) and new versions of the programming language (3.x). What we will do are: Encode the string to bytes using the desired encoding, such as encode("utf-8"). Call the binascii.hexlify() function to convert the bytes to a hexadecimal ...
🌐
Delft Stack
delftstack.com › home › howto › python › str to hex python
How to Convert String to Hex in Python | Delft Stack
February 2, 2024 - The result of the encode() method is a sequence of bytes, which can be further processed using various methods. For example, the hex() function can be applied to convert each byte into its hexadecimal representation.
🌐
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 - When you convert from bytes to str, you are decoding; when you convert from str to bytes, you are encoding. That is, the byte sequence is the "code" that represents the string's abstracted contents (code points, which in turn represent characters) In 3.x, we can decode by using the special-cased version of str that takes extra arguments:
🌐
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...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-hex-function
hex() function in Python - GeeksforGeeks
July 11, 2025 - 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. ... float.hex(3.9) converts the floating-point number 3.9 into its hexadecimal representation 0x1.f333333333333p+1.
🌐
Reddit
reddit.com › r/learnpython › converting string to ascii hex python 3
r/learnpython on Reddit: Converting string to ASCII HEX Python 3
November 22, 2020 -

Hi all,

I've been struggling with this for an hour or so, I'm trying to take a string such as

'Hell' to '\x48\x65\x6c\x6c'

For what I'm doing, I require the \x before the ascii hex

This is a dated solution from StackOverflow, that doesn't work on Python3

>>> mystring = "Hello World"
>>> print ''.join(r'\x{02:x}'.format(ord(c)) for c in mystring)
\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64                

My new solution goes like this:

mystring = b'hello world'

#convert to hex
mystring = hexlify(mystring)
mystring = mystring.decode('ascii')
# hex, like this 68656c6c6f20776f726c64

#add in the \x in the right spots
mystring = '\\x'.join(mystring[i:i + 2] for i in range(-2, len(mystring), 2)) 
#looks like this at this point \x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64

#I need it back into the "b" format, so I encode it 
mystring=mystring.encode(ascii)
# This breaks everything, now it becomes:
# b'\\x68\\x65\\x6c\\x6c\\x6f\\x20\\x77\\x6f\\x72\\x6c\\x64'
# I specifically can't use the double backslashes.

Any ideas how to fix this or am I going about this all wrong? Thanks

🌐
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.
🌐
AskPython
askpython.com › python › examples › convert-bytes-to-hex-strings
4 Handy Ways to Convert Bytes to Hex Strings in Python 3 - AskPython
May 16, 2023 - In Python 3, there are several ways to convert bytes to hex strings. You can use Python’s built-in modules like codecs, binascii, and struct or directly leverage the bytes.hex() function.
🌐
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 Python string variable string with the value “Goom”. We then use the encode() method on this string variable to convert it to bytes and store the result in a variable called bytes and that can 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.