Python Forum
Simple question about .format(* )
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question about .format(* )
#1
I am working with the following code and I am trying to understand what the purpose of the asterisk within the ".format(*formula)" operation on the last line of the script is. When I remove the asterisk, running the script gives the following error: "IndexError: tuple index out of range"

Can someone help me understand what the asterisk is? And what it is doing to allow the results of the function to print within the formatted string on the last line? Thanks for reading Cool



def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates = secret_formula(start_point)

#remember that this is another way to format a string
print("With a starting point of: {}".format(start_point))
#it's just like with an f"" string
print(f"We'd have {beans} beans, {jars} jars, and {crates} crates." )

start_point = start_point / 10

print("We can also do that this way:")
formula = secret_formula(start_point)
#this is an easier way to apply a list to a format string
print("We'd have {} beans, {} jars, and {} crates.".format(*formula))
Reply
#2
formula is three element tuple. asterisk "unpack" the tuple into separate elements, thus providing the necessary number of arguments to .format()

Note you can use two asterisk ** to "unpack" mapping like dictionary
person = {'name':'John', 'age':17}
print('{name} is {age} years old'.format(**person))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Question - ' defined as "a". ?' Ryan012 10 1,491 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,832 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,475 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Simple arithmetic question ebolisa 5 2,009 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,238 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,635 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  Question about formula implementation in general format Alienspecimen 0 1,635 Mar-01-2021, 08:39 PM
Last Post: Alienspecimen
  Simple question 1234 4 2,228 Dec-04-2020, 12:29 PM
Last Post: DeaD_EyE
  Simple Timer Question cranberrica 3 2,141 Jun-22-2020, 06:29 PM
Last Post: cranberrica

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020