We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
He's referring to the 0 position argument in format(), this can be very useful, however is mostly left out from simple formatting to not clutter the code.
See this formatting scenario:
print('{}dogbarksat{}!'.format('His','night'))
since 2 arguments are supplied and 2 spots are available to be populated, the output will be:
"His dog barks at night"
(no additional conversion will happen either)
But for the task here the code could look like in my suggestion:
I will keep it brief, but since I am reusing the variable "i", which is within the scope of format() and I don't want to type it in like '{}{}{}{}'.format(i, i, i, i), I just specified to use the 0 position argument every time, so I won't have to multiply code needlessly.
I am also using a variable to specify the width and align of each printed out "i" and then to also apply data type conversions on the fly. In {0:>{w}b} it means, that I want the 0 pos argument to be used (0), I want the ouput to be aligned to the right (>), I want it to have the specified width ({w} as width variable, this can also be set statically as a digit i.e. "2") and I want the data to be converted to binary on the fly.
String Formatting
You are viewing a single comment's thread. Return to all comments →
He's referring to the 0 position argument in format(), this can be very useful, however is mostly left out from simple formatting to not clutter the code.
See this formatting scenario:
since 2 arguments are supplied and 2 spots are available to be populated, the output will be: "His dog barks at night" (no additional conversion will happen either)
But for the task here the code could look like in my suggestion:
I will keep it brief, but since I am reusing the variable "i", which is within the scope of format() and I don't want to type it in like '{}{}{}{}'.format(i, i, i, i), I just specified to use the 0 position argument every time, so I won't have to multiply code needlessly.
I am also using a variable to specify the width and align of each printed out "i" and then to also apply data type conversions on the fly. In {0:>{w}b} it means, that I want the 0 pos argument to be used (0), I want the ouput to be aligned to the right (>), I want it to have the specified width ({w} as width variable, this can also be set statically as a digit i.e. "2") and I want the data to be converted to binary on the fly.
Take a look in the docs here for a really good explanation: https://www.programiz.com/python-programming/methods/string/format
I hope that helps.