pomogranet
pomogranet

Reputation: 21

Using string formatting to evaluate hexadecimal, binary, decimal and octal versions of a value

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:

Apologies for the lengthy question, I am a beginner and I did not seem to understand any other explanation.

Upvotes: 2

Views: 264

Answers (1)

Sébastien Lavoie
Sébastien Lavoie

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:

  • Decimal is base 10 (using numbers from 0 to 9, adding 1 and starting again when reaching the next value → ... 8, 9, 10, 11...).
  • Binary is base 2 (using numbers from 0 to 1, adding 1 and starting again when reaching the next value → 0, 1, 10, 11, 100...).
  • Hexadecimal is base 16 (using numbers from 0 to 9 and letters from A to F to form numbers of value from 0 to 15, adding an additional digit adds 16 → ... 9, A, B, C, D, E, F, 10, 11...).
  • Octal is base 8 (using numbers from 0 to 7, adding a digit each time we reach 7 → ... 6, 7, 10, 11, 12...).

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

Related Questions