You can use string formatting for this purpose:

>>> "0x{:02x}".format(13)
'0x0d'

>>> "0x{:02x}".format(131)
'0x83'

Edit: Your code suggests that you are trying to convert a string to a hexstring representation. There is a much easier way to do this (Python2.x):

>>> "abcd".encode("hex")
'61626364'

An alternative (that also works in Python 3.x) is the function binascii.hexlify().

Answer from Sven Marnach on Stack Overflow
🌐
LabEx
labex.io › tutorials › python-how-to-format-the-hexadecimal-output-in-python-417964
How to format the hexadecimal output in Python | LabEx
Default hex (lowercase): 0xab Hex in uppercase: 0XAB Hex without 0x prefix: ab Hex padded to 2 digits: ab Hex padded to 4 digits: 00ab · This demonstrates some simple ways to manipulate the hexadecimal output format in Python.
Discussions

Add length parameter to hex
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 ... More on discuss.python.org
🌐 discuss.python.org
3
0
April 23, 2023
hex - Python Hexadecimal - Stack Overflow
How to convert decimal to hex in the following format (at least two digits, zero-padded, without an 0x prefix)? Input: 255 Output:ff Input: 2 Output: 02 I tried hex(int)[2:] but it seems More on stackoverflow.com
🌐 stackoverflow.com
python - Formatting hex to be printed with 2 digits? - Stack Overflow
The built-in function hex() in python returns me a number in the form 0xf or 0x0 but i want them as 0x00. I found this on the python docs but dont really know how to interpret it. Thanks More on stackoverflow.com
🌐 stackoverflow.com
python - Format ints into string of hex - Stack Overflow
Each hex digit should be represented by two characters: 5 - "05", 16 - "10", etc. ... #!/usr/bin/env python def format_me(nums): result = "" for i in nums: if i <= 9: result += "0%x" % i else: result += "%x" % i return result print format_me([0,1,2,3,127,200,255]) More on stackoverflow.com
🌐 stackoverflow.com
🌐
Finxter
blog.finxter.com › home › learn python blog › python integer to hex — the ultimate guide
Python Integer to Hex - The Ultimate Guide - Be on the Right Side of Change
March 23, 2023 - One way to generate a 2-digit hexadecimal number is by using the built-in Python function hex(). However, this method might not always return a 2-digit hex number. To obtain the desired result, you can use the format() function:
🌐
Finxter
blog.finxter.com › home › learn python blog › format integer list to hex string – six pythonic ways
Format Integer List to Hex String - Six Pythonic Ways - Be on the Right Side of Change
November 23, 2022 - ints = [0, 1, 2, 3, 255, 254, 253] ... Tutorial: List Comprehension in Python — Ultimate Guide · The expression ''.join('x'%i for i in ints) converts each int from a list of ints into a single two-digit hex string using the ...
🌐
Python.org
discuss.python.org › python help
Add length parameter to hex - Python Help - Discussions on Python.org
April 23, 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 parameter in hex() to automatically pad output with 0 Signature will be changed to def hex(value, length: int | None = None) -> str: pass You would use it like so: >>> hex(13) # 0xd >>> hex...
🌐
Computer science
gcsecs.com › hexadecimal---2-digits.html
Hexadecimal - 2 digits - Computer science
Learning basic hex is easy, if you don't know how to do it, click here. If you have learned hex 1-15 then lets add another layer of complexity, for example the single digit "A" in hex is 10 in denary which is 1010 in binary .
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python
Format Strings and Numbers in Python: format() | note.nkmk.me
May 18, 2023 - In Python, you can convert numbers and strings to various formats with the built-in function format() or the string method str.format(). The built-in function: format() The string method: str.format() ...
🌐
pythontutorials
pythontutorials.net › blog › how-can-i-format-an-integer-to-a-two-digit-hex
How to Format an Integer as Two-Digit Hex in Python: Adding Leading Zeros for Hex String Concatenation — pythontutorials.net
Suppose you need to concatenate the hex representations of 5 (decimal) and 10 (decimal): Incorrect: If 5 is formatted as 5 (no leading zero) and 10 as a, concatenation gives 5a. Correct: If 5 is 05 and 10 is 0a, concatenation gives 050a (clearly representing two separate bytes). Missing leading zeros can break parsing or interpretation of concatenated data! Python offers several ways to format integers as two-digit hex strings with leading zeros.
🌐
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...
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
convert number to length-specified hex string - Raspberry Pi Forums
October 23, 2012 - string = '{:04x}'.format(n)The main advantage of this is that it has a name, so it is easier to find the documentation for it. The disadvantage is that the format string is just as impenetrable if you have not seen it before and, unlike the % format string, is completely python-specific rather than being the same as for sprintf in C, sh, Perl, PHP, Ruby, Octave…
🌐
GeeksforGeeks
geeksforgeeks.org › python-hex-function
hex() function in Python - GeeksforGeeks
May 9, 2025 - AND (a & b) and OR (a | b) operations on a (0x22) and b (0x0A), resulting in 0x02 (2 in decimal) and 0x2A (42 in decimal), respectively. hex(a & b) prints 0x2 and hex(a | b) prints 0x2a, converting the results into hexadecimal format.
🌐
LabEx
labex.io › tutorials › python-how-to-pad-hex-values-in-python-502158
How to pad hex values in Python | LabEx
graph TD A[Hex Value] --> B[16 Possible Digits] B --> C[0-9] B --> D[A-F] A --> E[Prefixed with 0x] A --> F[Case Insensitive] ... By understanding hex basics, you'll be well-prepared for more advanced Python programming techniques. LabEx recommends practicing these conversions to build proficiency. Hex padding ensures consistent length and formatting of hexadecimal values, which is crucial in various programming scenarios.
🌐
Real Python
realpython.com › ref › builtin-functions › hex
hex() | Python’s Built-in Functions – Real Python
Converting integer values to hexadecimal strings for use in digital systems, such as color codes in web development. Debugging or logging in systems that require hexadecimal notation. Preparing data for protocols or file formats that use hexadecimal representation. Let’s say you need to convert a list of decimal RGB values into their hexadecimal color code equivalents for use in web design. ... >>> rgb_values = [255, 165, 0] >>> # Strip '0x', pad to 2 digits >>> hex_colors = [hex(value)[2:].zfill(2) for value in rgb_values] >>> hex_color_code = '#' + ''.join(hex_colors) >>> hex_color_code '#ffa500'
🌐
GeeksforGeeks
geeksforgeeks.org › 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
April 4, 2023 - If the number is greater than 0, then perform the following steps: a. Calculate the remainder when the number is divided by 16. b. Add the corresponding hexadecimal digit from hex_digits to the beginning of hex_value. c. Right shift the number by 4 bits. 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)