FAQ: String Methods - Review


#1

This community-built FAQ covers the “Review” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Review

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2
highlighted_poems = "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"

highlighted_poems_list = highlighted_poems.split(",")


highlighted_poems_stripped = []

for poems in highlighted_poems_list:
  highlighted_poems_stripped.append(poems.strip())
  
highlighted_poems_details = []
for i in highlighted_poems_stripped:
  highlighted_poems_details.append(i.split(":"))


titles = []
poets = []
dates = []

for info in highlighted_poems_details:
  titles.append(info[0])
  poets.append(info[1])
  dates.append(info[2])

for poem in highlighted_poems_details:
  poem_string ="The poem {title} was published by {poet} in {date}".format(title=poem[0],poet=poem[1],date=poem[2])

print(poem_string)
  

Hi, I am currently struggling on the last part of the review excericise: 10.

Finally, write a for loop that uses either f-strings or .format() to prints out the following string for each poem: “The poem TITLE was published by POET in DATE”

The output is only the last set of information in the highlighted_poems_details list. What Im not sure is how come the only output is only the last set of information and not any others within the list?


#3

which won’t work in the LE. Wrong version.

3.5.2 (default, Nov 12 2018, 13:43:14)

This is because it is only one preserved. The instruction states to iterate over the list and print each item, which one would understand to happen inside the loop.


Funny thing, though. The instruction does not specify which object to iterate, only that we should construct output by the provided template. How might we write the for loop such that this format could be used?

print (str.format(title=x,poet=poets[i],date=dates[i]))

You may need to work on this question after passing (or getting the solution) so we can focus on output, rather than whether it is the solution or not.


#4

You’ve got it correct! Just indent your last print so it’s inside the for loop.


#6

I was confused approaching step 10 because step 9 asks us to assign poems, titles, and authors to separate lists. Anyone know how to use those new lists for step 10 using only string methods we’ve learned up to this point?


#7

So we’re clear, all three lists having corresponding indices. That is, the title at titles[0] corresponds with poets[0] and with dates[0]. This means we can iterate over one list, and use the index to access the other two.

for i in range(len(titles)):

where i is the index of the current iteration.

title, poet, date = titles[i], poets[i], dates[i]

Now just plug those variables into the format arguments and Bob’s your uncle.


#8

Awesome, you’ve made it clear. I was thinking iterations but I’ve taken a few weeks off of working through this python course (which I’m loving). Thanks!