Reputation: 21
I am a beginner at Python, attempting to understand how string formatting can help in listing the hexadecimal, octal, decimal, and binary versions of a number all in one line.
I am attempting to understand the logic of the code snippet provided.
"{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = width)
I do not understand how the above code works. Mainly:
what is the function of 0:
what is the function of {width} (is it for spacing)
what are d
, o
, X
, and b
and how are they being evaluated.
Apologies for the lengthy question, I am a beginner and I did not seem to understand any other explanation.
Upvotes: 2
Views: 264
Reputation: 897
The easiest way to get to know what a piece of code does is to execute it in a Python interpreter when appropriate (see this documentation if you don't know how the interpreter works). In this case, I suggest that you start by simply running that line and see what happens.
>>> "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(i,width = width)
...
NameError: name 'i' is not defined
First, you notice a name error, which means the variable i
is not assigned. Let's change it then to say, 42.
>>> "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(42,width = width)
...
NameError: name 'width' is not defined
Of course, we also need to indicate what width
is. Let's try with the value 8
.
>>> "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(42,width = 8)
' 42 52 2A 101010'
Now we get something that seems to work. What is width
, then? You can see it affects the spacing to the left of the different values that you get. Let's try changing it to 14
.
>>> "{0:{width}d} {0:{width}o} {0:{width}X} {0:{width}b}".format(42,width = 14)
' 42 52 2A 101010'
The space has indeed increased. Now, based on the title of your question, I will assume that you know what hexadecimal, binary, decimal and octal numbers are. As a very short refresher, just know that:
Where you might be wondering is: why does 0 (zero) appear at the beginning of each number representation? This is part of what is called string formatting as you mentioned. You will find more information on the subject in the official documentation. What this means here is that every time you have 0
in curly braces in a string, the first value passed to the .format()
function (in this case, 42
), will replace 0
and a format will be applied to it.
Here is an example of how it works. You don't have to limit yourself to using 0
. In fact, you can have other numbers like so:
>>> "{0} is a number, {1} is another number and {0} is the same as the first number.".format(12, 24)
'12 is a number, 24 is another number and 12 is the same as the first number.'
You could even skip numbers entirely inside the curly braces if you only have one value:
>>> "{} is a number.".format(12)
'12 is a number.'
To understand the right side in the curly braces, after the colon, you will need to check out the documentation I referenced earlier. But as a quick start, suppose you want to display a floating number with 2 decimal places. You would say .2
for a precision of 2 digits and f
to indicate float
. This would look like:
>>> "{0:.2f} is a number.".format(12)
'12.00 is a number.'
In your particular example, d
stands for decimal, o
stands for octal, X
stands for heXadecimal and b
stands for binary. You can see this is true because in the example given with 42
, 42
is indeed 42
in decimal, 52
in octal, 2A
in hexadecimal and 101010
in binary.
Upvotes: 1