Different Ways to Print Binary Values as Decimals in Python
1. Directly using the binary literal
print(0b101)
5
2. Using int function with a binary string
print(int('101', 2))
5
3. Using format function for binary to decimal conversion
print(int(format(0b101, 'd')))
5
4. Using f-strings (Python 3.6+)
binary_value = 0b101
print(f'{binary_value}')
5
5. Using str.format method
binary_value = 0b101
print('{}'.format(binary_value))
5
0 Comments:
Post a Comment