For printing some numbers to their binary formats, we simply use the .format()
method, like so:
# Binary
for i in range(5+1):
print("{0:>2} in binary is {0:>08b}".format(i))
0 in binary is 00000000
1 in binary is 00000001
2 in binary is 00000010
3 in binary is 00000011
4 in binary is 00000100
5 in binary is 00000101
Similar is for printing in other formats (hex and octal) which just requires replacing the latter braces to the digits we want to print. But is there a way to use the new f""
string to replace the .format()
command? I know this might seem trivial but I stumped over this while playing around with the new feature, besides f""
makes the code shorter and more readable.
for i in range(5+1):
print(f'{0:>2} in binary is {0:>08b}')
# This prints out just 0s
0
toi
here? – Anton vBR