String formatting query, what does "{0:b}".format(num) do?

pad = len(str("{0:b}".format(num)))*

please, explain the above code I could not understood the above the formatting method.

x = 'hi'
string_test = "{0}! nice to meet  you".format(x)"
# outputs > hi! nice to meet you

{0:b} just specifies that it will come out in binary format. so if {0} is an integer it’ll display as the binary equivalent instead.

For more info on formatting variants, check documentation or this link (

1 Like

I edited the title to be a little more descriptive.

String formatting in general is described at the following link-

To break it down a bit the {} specifies a section be filled by a a given variable name/identifier which only occurs when the code is run.

num = 100
"{}".format(num)  # Out: '100'
# converted integer that num referred to into a string

β€œ{:b}” is for converting the argument to a binary form.
Some examples-

"{0:b}".format(3)  # Out: '11'
"{0:b}".format(4)  # Out: '100'
"{0:b}".format(5)  # Out: '101'

The prefix of zero just specifies which argument should be used to fill the {}-

"{1} is the second argument. {0} is the first".format('yellow', 3)
# Out: '3 is the second argument. yellow is the first'
"{1:b}, {0:b}".format(0, 20)  # Out: '10100, 0'
# Note the argument order is switched
1 Like