Python Hexadecimal - How to convert an integer to the hexadecimal string
Python hex
converts an integer to the hex string. For example, 19 is 16 + 3 so 19 is converted to 0x13
. 0x
is a prefix meaning the value is base 16.
a = hex(19)
b = format(19, 'x')
print(a) # 0x13
print(type(a)) # <class 'str'>
print(b) # 13
print(type(b)) # <class 'str'>
If you want to remove 0x
prefix, format
is the best way. format
literally formats a value.
Example
a = hex(1023)
b = format(1023, 'x')
print(a) # 0x3ff
print(type(a)) # <class 'str'>
print(b) # 3ff
print(type(b)) # <class 'str'>
\[ 1023 = 16 \times 16 \times 3 + 16 \times 15 + 15 \]
Python String
- Check if a string contains the other string
- Check if a string starts with another string
- Split a string
- Concatenate strings
- Get the index of substring
- Get the index of the last occurrence of a substring
- Trim a string
- Replace a substring
- Replace multiple spaces with one space
- Fill 0 letters to a string
- Capitalize a string
- Convert a string lowercase
- Check if a string is numeric
- Check if a string is uppercase
- Parse a string to a float
- Format a string
- f-string
- Reverse a string
- Slice a string
- Subtract a string from another string
- Make a random string
- Generate a secure token
- Encode a string
- Get the sha256 value
- Convert an integer to a hexadecimal string
- Get a character or integer value
- Get digits or ASCII letters
- Center a string
- Convert string to tuple