+ 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

4th Oct 2022, 2:46 PM
Shantanu
Shantanu - avatar
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}'
4th Oct 2022, 5:33 PM
Lothar
Lothar - avatar
+ 7
0b indicates that it number is binary representation. You could simply print only the string from the 2nd index on.
4th Oct 2022, 2:55 PM
Lisa
Lisa - avatar
+ 4
Lothar ooooh that's nice!
5th Oct 2022, 5:44 AM
Oma Falk
Oma Falk - avatar
+ 2
Lothar i did not know this
5th Oct 2022, 5:54 AM
Harsha S
Harsha S - avatar