0

I'm trying to learn Python from the book: "Fluent Python, 2nd Ed" by Luciano Ramalho.

Example 2-8. Unpacking nested tuples to access the longitude

metro_areas = [
    ('Tokyo', 'JP', 36.933, (35.689722, 139.691667)),  
    ('Delhi NCR', 'IN', 21.935, (28.613889, 77.208889)),
    ('Mexico City', 'MX', 20.142, (19.433333, -99.133333)),
    ('New York-Newark', 'US', 20.104, (40.808611, -74.020386)),
    ('São Paulo', 'BR', 19.649, (-23.547778, -46.635833)),
]

def main():
    print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')
    for name, _, _, (lat, lon) in metro_areas:
        if lon <= 0:
            print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')

if __name__ == '__main__':
    main()

Here in print(f'{"":15} | {"latitude":>9} | {"longitude":>9}') I can't understand logic behind using {"":15}, {"latitude":>9, {"longitude":>9} part of the code. Can anyone can explain why the writer uses this in a print() function call?

The output:

                |   lat.    |   lon.
Mexico City     |   19.4333 |  -99.1333
New York-Newark |   40.8086 |  -74.0204
São Paulo       |  -23.5478 |  -46.6358
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Your question is about string formatting in that `print()` call, but your title indicates that you have a question about tuple unpacking. Please read [ask] and the [question checklist](//meta.stackoverflow.com/q/260648/843953). Titles are supposed to summarize the question – Pranav Hosangadi Aug 24 '21 at 17:23
  • https://docs.python.org/3/library/string.html#format-specification-mini-language – enzo Aug 24 '21 at 17:24
  • `{"":15}, {"latitude":>9, {"longitude":>9}` formats the column headings in fields of the specified width, so that they match the widths of the data rows. – Barmar Aug 24 '21 at 17:25
  • Title edited, If that was not upto standard, im learning how to ask question thanks for suggesting @PranavHosangadi – bashasha umer Aug 24 '21 at 17:26
  • 1
    It's just a so-called "f-string" or [formatted string litera](https://docs.python.org/3.6/whatsnew/3.6.html#pep-498-formatted-string-literals) where all the replacement fields are expressions that involves only string constants and no variables. – martineau Aug 24 '21 at 17:57

2 Answers2

2

The purpose is to print the table header with the same field widths as the actual table, so that they line up. Compare these lines:

print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')
print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')

Instead of variables (name, lat, lon), the author is passing literal strings ("", "latitude", "longitude") to the f-string format arguments instead. This is valid, because the part between {} can be any valid expression.

By using the same field widths (15, 9, 9) in both statements, the code ensures that the column headings have the same width as the actual table cells, so they are aligned in the output.

Thomas
  • 174,939
  • 50
  • 355
  • 478
1
print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')

Here the author is just formatting the values nicely to human readers. He's using what's knowns as f-strings which is used to interpolate strings.

You can place expressions inside curly braces of strings prefixed with an f, as in this case.

https://docs.python.org/3.9/library/string.html#format-specification-mini-language

The colon is used to apply a format specifier to the expression. In this case, the specifier > "Forces the field to be right-aligned within the available space", and the number 9 is the width for that specifier.

https://docs.python.org/3.9/library/string.html#format-specification-mini-language