
Quick answer: π
Use the f-string expression f'0x{val:X}'
to convert an integer val
to a hexadecimal string using uppercase hex letters as set by the capitalized hex format specifier X
. Optionally, you can add the prefix '0x'
in front of this.
Example:
>>> val = 3242342093423 >>> # Lowercase: >>> f'0x{val:x}' '0x2f2eaa6126f' >>> # Uppercase: >>> f'0x{val:X}' '0x2F2EAA6126F'
π‘ Note: This is Method 3 of this tutorial, and it is presented in more detail below.
Next, you’ll learn many more ways to solve the problem—because you may not particularly like (or understand) f-strings yet.
In any case, I’d encourage you to keep reading to learn and improve your skills and find out about different solutions that may be more intuitive for you!
Problem Formulation
π¬ Question: How to print a number as a hex string with the prefix '0x'
and uppercase letters A-F
instead of a-f
?

For example, when using hex()
or f-strings to convert an integer to a hex string, Python prints the hex string with lowercase letters a-f
to encode the hex digits 10-15
:
>>> val = 3242342093423 >>> hex(val) '0x2f2eaa6126f' >>> f'{val:#x}' '0x2f2eaa6126f'
What you want is an output with uppercase hex digits:
'0x2F2EAA6126F'
Method 1: Hex String Uppercase
You can convert the hex string with lowercase hex digits to a hex string with uppercase hex digits by calling the hex_str.upper()
method. This works because the type of the hex()
function output is a string. And converting a string to uppercase leaves all lowercase numbers intact.
Here’s to show you that the output of the hex()
function is a string type:
>>> val = 3242342093423 >>> type(hex(val)) <class 'str'>
Here’s what happens after converting the whole string to an uppercase hex string using hex(val).upper()
:
>>> val = 3242342093423 >>> hex(val).upper() '0X2F2EAA6126F'
However, you may not want to uppercase the '0X'
prefix indicating that it is a hexadecimal string. Keep reading to learn how to combine it with slicing and string concatenation to fix this! π
Method 2: Hex String Uppercase with ‘0x’ Lowercase Prefix
The expression '0x' + hex(val)[2:].upper()
concatenates the prefix '0x'
with the uppercased hex string using slicing to select all characters except the first two to avoid uppercasing the '0x'
prefix.
Here’s a minimal example:
>>> val = 3242342093423 >>> '0x' + hex(val)[2:].upper() '0x2F2EAA6126F'
This code snippet uses two interesting Python features—I recommend you have a look at them if you don’t know them already:
There are many ways you can do the string concatenation without the Addition +
Operator, for example, using the print()
function with comma-separated arguments:
print('0x', hex(val)[2:].upper()) # 0x2F2EAA6126F
Related video you may be interested in:
π Recommended Tutorial: An Introduction to Python Slicing
Method 3: Python f-String
Use the f-string expression f'0x{val:X}'
to convert an integer val
to a hexadecimal string using uppercase hex letters as set by the capitalized hex format specifier X
. Optionally, you can add the prefix '0x'
in front of this.
Here’s an example that solves the problem as shortly and concisely as humanly possible:
>>> val = 3242342093423 >>> f'0x{val:X}' '0x2F2EAA6126F'
Note that if you used the lowercased format specifier x
after the colon, Python would convert to a lowercase hexadecimal string:
>>> f'0x{val:x}' '0x2f2eaa6126f'
Python is powerful, isn’t it? πͺ
Method 4: Oldschool String Formatting
Note that you can also use the other string format specifiers format()
and the %
notation in case you’re not using Python 3 or you’re a real old-school programmer:
>>> val = 3242342093423 >>> '0x{:X}'.format(val) '0x2F2EAA6126F' >>> '0x%X' % val '0x2F2EAA6126F'
π Recommended Tutorial: String format()
and Percentage %
Operator for String Formatting
Method 5: String Replace and Handling Negative Numbers
You can chain the outputs of hex()
, upper()
, and replace()
to obtain an uppercased hexadecimal representation of an integer val
without uppercasing the '0x'
prefix using the expression hex(val).upper().replace('X', 'x')
. This handles negative numbers correctly too!
Here’s our minimal example:
>>> val = 3242342093423 >>> hex(val).upper().replace('X', 'x') '0x2F2EAA6126F'
Here’s how this approach handles negative numbers:
>>> hex(-val).upper().replace('X', 'x') '-0x2F2EAA6126F'
Where to Go From Here

β€οΈ Thanks for reading the whole tutorial. I’d love to keep you in the community as an ambitious learner, so you can improve your skills and become a better coder!
Feel free to join our free email academy and download our free cheat sheets here: