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!


#9

I’ve got the following code which curiously seems to work in Visual Studio Code but gives a Syntax error on the last print line when trying to run it on the Codeacademy platform. Is there something wrong with the code? Would appreciate any guidance…

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 = [item.strip() for item in highlighted_poems_list]


highlighted_poems_details = []
highlighted_poems_details = [item.split(":") for item in highlighted_poems_stripped ]
print(highlighted_poems_details)

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

titles =[list[0] for list in highlighted_poems_details]
poets =[list[1] for list in highlighted_poems_details]
dates =[list[2] for list in highlighted_poems_details]
print(titles)

for i in range(len(titles)):
   print(f"the poem {titles[i]} was published by {poets[i]} in {dates[i]}")

#10

For this exercise we cannot use f-string interpolation since it requires version 3.6 (or 3.7?). The LE version predates that. Try using .format().

Since you are using comprehensions to build the lists, those three lines are not needed.


#11

Hi, guys, for some reason task-10 is getting completed if you hit “Run” no matter what code you wrote after completing task-9. I faced this bug, while trying to intentionally screw up and get the solution.
The other thing is, i didn’t quite remember learning f-string method and task-10 offers to use it.

With help of @mtf i finally get how to combine lists we previously created with iteration through them.

for i in range(len(titles)):
  title = titles[i]
  poet = poets[i]
  date = dates[i]
  glorious_output_string = "The poem \"{title}\" was published by {poet} in {date}.".format(title=title, poet=poet, date=date)
  print(glorious_output_string)

Thanks! :nerd_face:


#12

This was not introduced until Python 3.6 and will not work in earlier verrsions. The LE may be Python 3.5.


#13

I’am a bit confused, am i expected to know this method?

What does LE stand for?


#14

Learning Environment

Study up on all three methods of string interpolation. If you are unable to use the f-string, then assume the version of Python is before Python 3.6. It won’t matter which method you use in most exercises.


#15

I want to study up all three methods! :grinning: But LE gave me just .format and .format with KEY words

I just don’t get it. Why offer a student this f-string method he could use, but not teach him how. :thinking:


#16

That’s a question for CC. Can’t possibly answer it from where we stand (volunteers on the forum).