How to unpack and print a tuple

Dennis

I am a beginner in Python (3.x), this is my first post here, and I am trying to unpack a tuple, then print it with a certain format (my words may not be quite right). Here is what I have:

w = 45667.778
x = 56785.55
y = 34529.4568
z = 789612.554

nums = (w, x, y, z)
hdr62 = '{:>15} {:>15} {:>15} {:>15}'

print(tuple('${:,.0f}'.format(num).rjust(12, ' ') for num in nums))

What/how do I alter to unpack and then use the hdr62 format to print the tuple?

poke

Maybe I’m missing things, but if you just want to format the tuple nums according to the hdr62 format, then you can just call str.format on it and pass in the numbers as unpacked tuple:

>>> print(hdr62.format(*nums))
      45667.778        56785.55      34529.4568      789612.554

If you actually want to run another format on the tuple items first, you can do that separately:

>>> ['${:,.0f}'.format(n) for n in nums]
['$45,668', '$56,786', '$34,529', '$789,613']
>>> print(hdr62.format(*['${:,.0f}'.format(n) for n in nums]))
        $45,668         $56,786         $34,529        $789,613

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to 'unpack' a list or tuple in Python

From Dev

How to unpack optional items from a tuple?

From Dev

How to unpack a tuple from left to right?

From Dev

How to unpack optional items from a tuple?

From Dev

Unpack parts of a tuple into tuple

From Dev

How to unpack a tuple into more values than the tuple has?

From Dev

How can I unpack a list or tuple in place in Python?

From Dev

How to use a map with *args to unpack a tuple in a python function call

From Dev

Unpack Tuple within dictionary

From Dev

Python how to print tuple without quotes

From Dev

how to print lists within a tuple in straight line

From Dev

how to print a tuple of tuples without brackets

From Dev

How to print a returned tuple in another method

From Dev

Is there any way to unpack an iterator into a tuple?

From Dev

psycopg2 selecting timestamp returns datetime.datetime wrapped in tuple, how to unpack?

From Dev

python ValueError: too many values to unpack in tuple

From Dev

Too many to unpack tuple NLTK Chat

From Dev

How to print every item in a tuple of tuples in different order

From Dev

how can I print dictionary whose key is tuple and value is tuple in table format?

From Dev

print tuple beautifully with newline

From Dev

Anatomy of pretty print tuple

From Dev

How to fix this unpack issue?

From Dev

How to unpack a msgpack file?

From Java

How to unpack an .asar file?

From Java

How to unpack pkl file?

From Dev

How to unpack mnist dataset?

From Dev

how to unpack array into variables

From Dev

How to unpack this list comprehension

From Dev

how to unpack the list of elements