Since Python 3.6+ with an f string:

>>> '{:^14s}'.format(f'0x{data:02x}')
'     0x0a     '

Which can be (perhaps abusively) shortened to:

>>> f'{f"0x{data:02x}":^14}'
'     0x0a     '

And perhaps a little more straightforwardly:

>>> f'{format(data, "#04x"):^14}'
'     0x0a     '

With Python<3.6:

>>> '{:^14s}'.format('{:#04x}'.format(data))
'     0x0a     '
Answer from dawg on Stack Overflow
๐ŸŒ
Myshell
myshell.co.uk โ€บ blog โ€บ 2018 โ€บ 11 โ€บ python-f-string-formatting-cheatsheet
Python: f-string formatting cheatsheet :: myshell.co.uk
November 5, 2018 - Note: All examples below were executed in Python interactive shell (REPL - Read, Eval, Print and Loop). [[fill]align][sign][#][0][width][grouping_option][.precision][type]
Discussions

You should know these f-string tricks
print(f"number: {number:,}") puts commas in large numbers. More on reddit.com
๐ŸŒ r/Python
182
2006
October 28, 2023
[`ruff`] Formatting hex codes changes output with f-string debug
I had this idea while reading psf/black#4522 when @MichaReiser said ruff had already stabilized hex code formatting. I don't actively use ruff/am not familiar with it, but I assume that just li... More on github.com
๐ŸŒ github.com
10
March 30, 2024
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_string_formatting.asp
Python String Formatting
Before Python 3.6 we had to use the format() method. F-string allows you to format selected parts of a string.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ string.html
string โ€” Common string operations
This option is only valid for integer, float and complex types. For integers, when binary, octal, or hexadecimal output is used, this option adds the respective prefix '0b', '0o', '0x', or '0X' to the output value.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ string-formatting
Python f-string tips & cheat sheets - Python Morsels
April 12, 2022 - Python also has format specifiers for representing numbers in binary or hexadecimal.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Add length parameter to hex - Python Help - Discussions on Python.org
April 22, 2023 - Hey everyone! During my wrok I need to convert int to hex and send it to a server. The problem is that data I send must be paired. To achieve this I write code like this: value_to_send = hex(value)[2:] if len(value_to_send) % 2: value_to_send = '0' + value_to_send My suggestion is to add optional ...
๐ŸŒ
Reddit
reddit.com โ€บ r/python โ€บ you should know these f-string tricks
r/Python on Reddit: You should know these f-string tricks
October 28, 2023 -

F-strings are faster than the other string formatting methods and are easier to read and use. Here are some tricks you may not have known.

1. Number formatting :

You can do various formatting with numbers.

>>> number = 150

>>> # decimal places to n -> .nf
>>> print(f"number: {number:.2f}")
number: 150.00

>>> # hex conversion
>>> print(f"hex: {number:#0x}")
hex: 0x96

>>> # binary conversion
>>> print(f"binary: {number:b}")
binary: 10010110

>>> # octal conversion
>>> print(f"octal: {number:o}")
octal: 226

>>> # scientific notation
>>> print(f"scientific: {number:e}")
scientific: 1.500000e+02

>>> # total number of characters
>>> print(f"Number: {number:09}")
Number: 000000150

>>> ratio = 1 / 2
>>> # percentage with 2 decimal places
>>> print(f"percentage = {ratio:.2%}")
percentage = 50.00%

2. Stop writing print(fโ€var = {var}โ€)

This is the debug feature with f-strings. This is known as self-documenting expression released in Python 3.8 .

>>> a, b = 5, 15
>>> print(f"a = {a}") # Doing this ?
a = 5
>>> # Do this instead.
>>> print(f"{a = }")
a = 5
>>> # Arithmatic operations
>>> print(f"{a + b = }")
a + b = 20
>>> # with formatting
>>> print(f"{a + b = :.2f}")
a + b = 20.00

3. Date formatting

You can do strftime() formattings from f-string.

>>> import datetime

>>> today = datetime.datetime.now()
>>> print(f"datetime : {today}")
datetime : 2023-10-27 11:05:40.282314

>>> print(f"date time: {today:%m/%d/%Y %H:%M:%S}")
date time: 10/27/2023 11:05:40

>>> print(f"date: {today:%m/%d/%Y}")
date: 10/27/2023

>>> print(f"time: {today:%H:%M:%S %p}")
time: 11:05:40 AM

Check more formatting options.

Part 2 - https://www.reddit.com/r/Python/s/Tzx7QQwa7A

Thank you for reading!

Comment down other tricks you know.
Find elsewhere
๐ŸŒ
Fstring
fstring.help
fstring.help: Python f-string guide
Cheat sheet tables can be found at fstring.help/cheat thanks to Trey Hunner. Repository on Github, contributions welcome! If you prefer an interactive version, . f-strings are strings with an f in front of them: f"..." or f'...'. Inside the f-string, curly braces can be used to format values ...
๐ŸŒ
HexDocs
hexdocs.pm โ€บ f_strings
F-strings v0.1.0 โ€” Documentation
We cannot provide a description for this page right now
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ f-string python hex, oct, and bin: efficient number conversions
F-String Python Hex, Oct, and Bin: Efficient Number Conversions - Be on the Right Side of Change
March 27, 2023 - They improve upon the older string formatting methods, such as using %-formatting and the str.format() method. One of the key advantages of f-strings is their ability to handle various numeric representations, such as hexadecimal (hex), octal (oct), ...
๐ŸŒ
GitHub
github.com โ€บ astral-sh โ€บ ruff โ€บ issues โ€บ 14766
[`ruff`] Formatting hex codes changes output with f-string debug ยท Issue #14766 ยท astral-sh/ruff
March 30, 2024 - bugSomething isn't workingSomething isn't workingformatterRelated to the formatterRelated to the formatter ... I had this idea while reading psf/black#4522 when @MichaReiser said ruff had already stabilized hex code formatting.
Author ย  MeGaGiGaGon
๐ŸŒ
YouTube
youtube.com โ€บ shorts โ€บ S-swX1rzwa4
How To Display A Number As Hex Using F-Strings - YouTube
In this python tutorial, I show you how to display a number as hex using f-strings. Let's get coding!======== Ask Case Digital ========If you have a question...
Published ย  December 4, 2024
๐ŸŒ
w3resource
w3resource.com โ€บ python โ€บ built-in-function โ€บ hex.php
Python hex() function - w3resource
September 20, 2024 - The function returns the hexadecimal representation of an integer as a string, prefixed with "0x".
๐ŸŒ
Medium
medium.com โ€บ @nawazmohtashim โ€บ convert-hex-to-string-in-python-a-comprehensive-guide-36d4f128cd9e
Convert Hex to String in Python: A Comprehensive Guide | by Mohd Mohtashim Nawaz | Medium
December 13, 2023 - Before diving into the conversion process, letโ€™s briefly understand how hexadecimal representation works. Hexadecimal uses 16 symbols to represent values: 0โ€“9 and A-F, where A stands for 10, B for 11, and so on, up to F for 15.
๐ŸŒ
YouTube
youtube.com โ€บ codestack
python f string hex - YouTube
Download this code from https://codegive.com Title: Python F-String Hex Formatting: A Comprehensive Tutorial with Code ExamplesPython's f-strings provide a c...
Published ย  December 14, 2023
Views ย  0
๐ŸŒ
Techieclues
techieclues.com โ€บ blogs โ€บ converting-an-integer-to-a-hexadecimal-string-in-python
Converting an Integer to a Hexadecimal String in Python
December 14, 2025 - Oops! Looks like the page you are looking for is not available or moved to a new section ยท We have upgraded our website and open for developer community. You can share your technical knowledge with everyone by posting Articles, Blogs or Interview Questions and Answers
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ print hex with uppercase letters in python
Print Hex with Uppercase Letters in Python - Be on the Right Side of Change
November 13, 2022 - 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.
๐ŸŒ
Podprecelom
gf474kt.podprecelom.ru
Python String Format Hex
We cannot provide a description for this page right now
๐ŸŒ
Itecnote
itecnote.com โ€บ tecnote โ€บ python-using-format-f-string-to-output-hex-with-0-padding-and-center
using format/f string to output hex with 0 padding AND center
React-native โ€“ React Native Error: โ€œAnimated.event now requires a second argument for optionsโ€ ยท Python โ€“ keep on getting โ€œx and y must have same first dimension, but have shapes (100,) and (1, 100)โ€