You mean you have a string of bytes in my_hex which you want to print out as hex numbers, right? E.g., let's take your example:

>>> my_string = "deadbeef"
>>> my_hex = my_string.decode('hex')  # python 2 only
>>> print my_hex
Þ ­ ¾ ï

This construction only works on Python 2; but you could write the same string as a literal, in either Python 2 or Python 3, like this:

my_hex = "\xde\xad\xbe\xef"

So, to the answer. Here's one way to print the bytes as hex integers:

>>> print " ".join(hex(ord(n)) for n in my_hex)
0xde 0xad 0xbe 0xef

The comprehension breaks the string into bytes, ord() converts each byte to the corresponding integer, and hex() formats each integer in the from 0x##. Then we add spaces in between.

Bonus: If you use this method with unicode strings (or Python 3 strings), the comprehension will give you unicode characters (not bytes), and you'll get the appropriate hex values even if they're larger than two digits.

Addendum: Byte strings

In Python 3 it is more likely you'll want to do this with a byte string; in that case, the comprehension already returns ints, so you have to leave out the ord() part and simply call hex() on them:

>>> my_hex = b'\xde\xad\xbe\xef'
>>> print(" ".join(hex(n) for n in my_hex))
0xde 0xad 0xbe 0xef
Answer from alexis on Stack Overflow
🌐
w3resource
w3resource.com › python › built-in-function › hex.php
Python hex() function - w3resource
April 18, 2026 - Available in: Python 3.2.5 and later · Syntax: hex(x) Parameter: Return Value: The function returns the hexadecimal representation of an integer as a string, prefixed with "0x". Example: Using hex() with integers · number = 127 print(number, ...
Discussions

Unusal behavior of python3* print hex values
All I want was to print an output of 4 values given in hex: This is OK and works how it should be: Python 2.7.18 (default, Mar 28 2022, 20:47:09) [GCC 11.2.0] on linux2 $ python2.7 -c ‘print ("\x37\x8a\x04\x08");’ | hexdump -C 00000000 37 8a 04 08 0a |7…| This is my problem: Python 3.10.4 ... More on discuss.python.org
🌐 discuss.python.org
9
0
May 2, 2022
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
Difference in print output for hex value (\x escaped) characters between Python 2 and Python 3?
In Python 2, strings are raw bytes (like char* from C/C++). In Python 3, strings are unicode-aware and there is a separate bytes type. You can get the ï¾­Þ in Python 2 by using a unicode literal or from __future__ import unicode_literals. Note the string itself is not unicode-aware, but it's at least encoded for you. $ python2 >>> print('\xef\xbe\xad\xde') ᆳ >>> print(u'\xef\xbe\xad\xde') ï¾­Þ >>> You can get the ᆳ in Python 3 by manually decoding the bytes object into a unicode-aware string. Note that \xef\xbe\xad\xde is not valid unicode so you'll have to ignore errors explicitly. $ python3 >>> print('\xef\xbe\xad\xde') ï¾­Þ >>> print(b'\xef\xbe\xad\xde'.decode(errors='ignore')) ᆳ >>> In general you should not worry about this in Python 3; just stick to unicode the whole time and things will work as you expect. If you must deal with bytes then you should be clear about it with str.encode(), bytes.decode(), and type hints. More on reddit.com
🌐 r/learnpython
5
1
October 29, 2022
Printing hexadecimals
When I use hex() or hexadecimal format with print with the value returned by mem32, if the most significant bit is 1, then I get a signed string. The only way I could find to print the value as an ... More on github.com
🌐 github.com
2
1
February 12, 2023
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-print-the-hexadecimal-value-of-the-numbers-from-1-to-n
Python program to print the hexadecimal value of the numbers from 1 to N - GeeksforGeeks
July 23, 2025 - Print the hexadecimal value for each number. Below is the implementation of the above approach: ... # Python program for the above approach # Function to print the hexadecimal number # from 1 to N def printHexadecimalNumber(N): hex_digits = "0123456789ABCDEF" # Iterate over the range for num in range(1, N+1): hex_value = "" while num > 0: rem = num % 16 hex_value = hex_digits[rem] + hex_value # Shift the number by 4 bits num >>= 4 # Print the hex value print(hex_value) # Driver Code N = 11 printHexadecimalNumber(N)
🌐
Python.org
discuss.python.org › python help
Unusal behavior of python3* print hex values - Python Help - Discussions on Python.org
May 2, 2022 - I’m new to python but have expierences on other languages. I discoverd an unusal behavior of python3* on my kali linux. All I want was to print an output of 4 values given in hex: This is OK and works how it should be: Python 2.7.18 (default, Mar 28 2022, 20:47:09) [GCC 11.2.0] on linux2 $ python2.7 -c ‘print ("\x37\x8a\x04\x08");’ | hexdump -C 00000000 37 8a 04 08 0a |7…| This is my problem: Python 3.10.4 (main, Mar 24 2022, 13:07:27) [GCC 11.2.0] on l...
🌐
W3Schools
w3schools.com › python › ref_func_hex.asp
Python hex() Function
Python Examples Python Compiler ... Study Plan Python Interview Q&A Python Training ... The hex() function converts the specified number into a hexadecimal value....
🌐
Real Python
realpython.com › ref › builtin-functions › hex
hex() | Python’s Built-in Functions – Real Python
Returns a string holding the lowercase hexadecimal representation of the input integer, prefixed with 0x.
Find elsewhere
🌐
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.
🌐
Quora
quora.com › In-Python-how-do-I-use-hex-value-to-print-a-hexadecimal-String-with-0-s-padded-to-the-left-and-without-0X-in-front
In Python: how do I use hex (value) to print a hexadecimal String with 0’s padded to the left and without “0X” in front? - Quora
Answer (1 of 4): Lukas has already given you an excellent answer, but, for completeness, I feel obliged to point out that you can print hex in a somewhat more flexible way using str.format() as below where the 0 is the fill character, 6 is the field width and X says you want hex format in upperca...
🌐
Reddit
reddit.com › r/learnpython › difference in print output for hex value (\x escaped) characters between python 2 and python 3?
r/learnpython on Reddit: Difference in print output for hex value (\x escaped) characters between Python 2 and Python 3?
October 29, 2022 -

Hello, I'm just trying to understand a difference between Python 2 and Python 3 when printing out values for \x escaped characters. Let me present a simple case where, as I would expect, the output between Python 2 and Python 3 is the same:

# Python 2
print '\x31\x32\x33\x34' # The hex equivalent of '1234'
# Outputs: 1234

# Python 3
print('\x31\x32\x33\x34')
# Outputs: 1234

Now here is an example where the output is unexpectedly (to me) different:

# Python 2
print '\xef\xbe\xad\xde' # The hex value 0xdeadbeef (little endian)
# Outputs: ᆳ

# Python 3
print('\xef\xbe\xad\xde')
# Outputs: ï¾­Þ

Why, in this case, are the outputs different?

🌐
Programiz
programiz.com › python-programming › methods › built-in › hex
Python hex()
Online Python Online JavaScript ... Go Online Rust Online Scala Online Dart Online R Online Ruby ... hex() function converts an integer to the corresponding hexadecimal number in string form and returns it....
🌐
Unicode
unicode.org › emoji › charts › full-emoji-list.html
Full Emoji List, v18.0
Index & Help | Images & Rights | Spec | Proposing Additions · This chart provides a list of the Unicode emoji characters and sequences, with images from different vendors, CLDR name, date, source, and keywords. The ordering of the emoji and the annotations are based on Unicode CLDR data.
🌐
TutorialsPoint
tutorialspoint.com › how-do-i-specify-hexadecimal-and-octal-integers-in-python
How to Convert Decimal to Binary, Octal, and Hexadecimal using Python?
September 16, 2022 - These functions are bin() for binary, ... = 27 print(bin(decimal), "in binary.") print(oct(decimal), "in octal.") print(hex(decimal), "in hexadecimal.") 0b11011 in binary....
🌐
LabEx
labex.io › tutorials › python-how-to-work-with-hex-representations-502160
How to work with hex representations | LabEx
## Padding hex values value = 15 ## Ensure 2-digit hex representation padded_hex = f'{value:02x}' print(padded_hex) ## Outputs: 0f ## Formatting hex with specific width formatted_hex = f'{value:04X}' print(formatted_hex) ## Outputs: 000F · When performing hex operations in LabEx: Use built-in Python bitwise operators ·
🌐
Nerd Fonts
nerdfonts.com › cheat-sheet
Nerd Fonts - Iconic font aggregator, glyphs/icons collection, & fonts patcher
Iconic font aggregator, collection, & patcher: 10,000+ glyph/icons, 70+ patched fonts: Hack, Source Code Pro, more. Popular glyph collections: Font Awesome, Octicons, Material Design Icons, and more
🌐
Java2Blog
java2blog.com › home › python › print bytes as hex in python
Print bytes as hex in Python - Java2Blog
September 26, 2022 - To print Bytes as Hex in Python, we will loop over a Byte string and get the Hexadecimal representation of each character individually.
🌐
LabEx
labex.io › tutorials › python-how-to-format-the-hexadecimal-output-in-python-417964
How to format the hexadecimal output in Python | LabEx
Let's create a simple Python script to understand hexadecimal better: Open the WebIDE (VSCode) interface in your LabEx environment. Create a new file named hex_basics.py in the /home/labex/project directory. ... ## Understanding decimal, binary, and hexadecimal conversion decimal_value = 42 ## Convert decimal to binary binary_value = bin(decimal_value) ## Convert decimal to hexadecimal hex_value = hex(decimal_value) ## Print all representations print(f"Decimal: {decimal_value}") print(f"Binary: {binary_value}") print(f"Hexadecimal: {hex_value}") ## Let's see a larger number large_decimal = 255 print(f"\nDecimal: {large_decimal}") print(f"Binary: {bin(large_decimal)}") print(f"Hexadecimal: {hex(large_decimal)}")