Learn Python 3: Help with this exercise using .format() method

Can someone explain to me why my code isn’t working? It seems to me it should and yet…
It comes from HERE (at point n. 10) in the ‘Learn Python 3’ course.

We have a list master_lst containing sublists that each contain the TITLE, POET and DATE of famous poems.
And as you can read, we are asked to:
" write a for loop that uses .format() to print out the following string for each poem:

The poem TITLE was published by POET in DATE."

master_lst = [['Afterimages', 'Audre Lorde', '1997'], ['The Shadow', 'William Carlos Williams', '1915'], ['Ecstasy', 'Gabriela Mistral', '1925'], ['Georgia Dusk', 'Jean Toomer', '1923'], ['Parting Before Daybreak', 'An Qi', '2014'], ['The Untold Want', 'Walt Whitman', '1871'], ["Mr. Grumpledump's Song", 'Shel Silverstein', '2004'], ['Angel Sound Mexico City', 'Carmen Boullosa', '2013'], ['In Love', 'Kamala Suraiyya', '1965'], ['Dream Variations', 'Langston Hughes', '1994'], ['Dreamwood', 'Adrienne Rich', '1987']]


string = "The poem {} was published by {} in {}"

for lst in master_lst:   
  string.format(lst[0], lst[1], lst[2])
  print(string)

That whole thing should be in the print statement. As it stands nothing is happening on that line.

Right…I should have mentioned that I had figured out a solution, but I just wasn’t clear as to why this other one I posted didn’t work as the logic seemed sound to me and couldn’t figure out what was wrong?

Now I finally get it…strings are immutable so of course no permanent formatting is done on string. Therefore yes, the print statement or storing it in a variable and then printing as I had done it does the trick.
Thx!

1 Like