+ 3
What is "0b" in the output of this code and can it be excluded from the output ?
print(bin(10)) print(bin(20)) output: 0b1010 0b10100
4 Respostas
+ 11
Shantanu ,
binary representation can also be done by using string interpolation with f-string. this sample is from the python documentation:
print(f'{14:#b} - {14:b}') # 'b' is the format specifier for binary
# result: '0b1110 - 1110'
so we can decide if we are using the prefix '0b' or not.
[EDITED] added a sample
to get the binary representation stored in a variable, we can use:
res= f'{14:b}'
+ 7
0b indicates that it number is binary representation. You could simply print only the string from the 2nd index on.
+ 4
Lothar ooooh that's nice!
+ 2
Lothar
i did not know this