Table of Contents
A hex
or a hexadecimal
string is one of the few forms of data types provided by Python for storing and representing data. A hex
string is generally represented with a prefix of 0x
. However, we can get rid of this 0x
and print just the original hex
value.
This tutorial focuses on and demonstrates the several ways available to print hex without 0x
in Python.
How to print hex without 0x in Python?
There are several different ways to print hex without 0x
in Python, some of the most common ones are described thoroughly in this article below.
There are several ways to convert a data type to hex
representation, with most of them providing a direct way to print the hex
representation without the 0x
prefix. Moreover, the other functions that do not provide a way directly can also be tweaked.
As an example in this tutorial, we will take a variable of the data type int
and represent the data stored in it in the hex
format without the use of the 0x
prefix.
Using slicing along with the hex()
function
A concept that is highly effective in this case is string slicing. It is a way to get a sub-string
from any given string by slicing it either from the beginning or from the end.
After utilizing the hex()
function on the given int
value, the obtained hexadecimal
or simply hex
string can be sliced to remove the 0x
prefix while utilizing the print command.
The following code uses slicing along with the hex()
function to print hex without 0x
in Python.
1 2 3 4 |
a = hex(2022) print(a[2:]) |
The above code provides the following output:
Using the replace()
function along with the hex()
function
The replace()
function is utilized to replace any specified phrase with another. Both the phrase to be specified and the replacement are passed as arguments to this function.
In our case, after making use of the hex()
function, we can simply replace the phrase x
of the hex
string and insert a 0
in place of it.
The following code uses the replace() function along with the hex() function to print hex without 0x
in Python.
1 2 3 4 |
a = hex(2022) print(a.replace('x', '0')) |
The above code provides the following output:
As leading 0s
do not hinder or change the value of a number, replacing x
with 0
is perfectly valid from a logical perspective. It might not give the most aesthetic results as the string would then start with two leading 0s
, but it is successful in providing a correct answer.
Using slicing and the zfill() function along with the hex() function
The zfill()
method in Python is utilized to add a trail of leading 0s
to a string. The number of zeroes to be added is specified and passed as an argument to this function.
Post the utilization of the hex()
function, the obtained hex
string can simply be sliced and then a trail of leading zeroes can be added to the string in case of the 0x
that got removed.
The following code uses slicing and the zfill()
function along with the hex() function to print hex without 0x
in Python.
1 2 3 4 |
a = hex(2022) print(a[2:].zfill(4)) |
The above code provides the following output:
Using the format()
function
The format()
function is one of the oldest ways in the book to implement string formatting in Python. It is versatile and works in both new and old versions of Python. The format()
string is also another method that could return a hex
string by converting it from any other data type.
The format()
function uses the #x
specifier to convert an int
to a hex
string. If we use the generic x
specifier instead, it returns a hex
string that does not have the 0x
prefix.
The following code uses the format() function to print hex without 0x
in Python.
1 2 3 4 5 |
a = 2022 z = format(a, 'x') print(z) |
The above code provides the following output:
Using %x
conversion
The %x
conversion is an old method to implement string formatting that was later discontinued after the introduction of Python 3.6. However, it is still a useful method if an older version of Python is being utilized by the reader.
Much like the format()
function, this version of string formatting also makes use of specifiers and the %x
sign here is a specifier. We should note that if we use %#x
, then the obtained hex
string contains the 0x
prefix, but if we only use the %x
sign then we do not get the 0x
prefix in the obtained string. This comes in handy in our case as we do not need the prefix but just the standard hex
string.
The following code uses the %x
conversion to print hex without 0x
in Python.
1 2 3 4 5 |
a = 2022 z = ("%x" % a) print(z) |
The above code provides the following output:
Further reading:
Using f-strings
F-strings were introduced with Python 3.6 and have quickly become the go-to method to implement string formatting in Python. It holds a lot of advantages over the other methods as it is simpler to implement and is quicker than its peers.
F-strings make use of curly braces which enclose the given int
value and the format specifier, both of which are separated by a colon.
The following code uses f-strings to print hex without 0x
in Python.
1 2 3 4 5 |
a = 2022 z = f'{a:x}' print(z) |
The above code provides the following output:
We should note that this method works only on compilers running a version of Python higher than or the same as Python 3.6.
Using the to_bytes()
function along with the hex() function
The to_bytes()
function, as the name suggests, is utilized to convert a given value into the bytes
data type. It can be utilized along with the hex()
function to print hex without 0x
in Python.
When the given int
value is first converted into a byte
value with the to_bytes()
function and is then converted to a hex
value afterward, it does not retain the 0x
prefix that the hex()
function usually returns along with the converted string.
The following code uses the to_bytes() function along with the hex() function to print hex without 0x
in Python.
1 2 3 4 |
a = 2022 print(a.to_bytes(2, "big").hex()) |
The above code provides the following output:
Conclusion.
This article provides seven different approaches to implement the task of printing hex without 0x
in Python, with all of them having their own advantages and disadvantages. The user can choose any method according to their needs as all the methods mentioned above provide an accurate answer without any hassle.