# FAQ: String Methods - Review

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:

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

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

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!

1 Like
``````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:

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?

2 Likes

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.

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

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?

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.

3 Likes

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!

2 Likes

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)):
``````
1 Like

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.

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!

1 Like

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

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

What does LE stand for?

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.

1 Like

I want to study up all three methods! 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.

1 Like

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

2 Likes

Hi, after practicing this review, I wanna print out the last thing which is “The poem TITLE was published by POET in DATE”

However, when I tried to print out the output, it showed nothing.
Here is my code:

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

def everthing_combine(titles, poets, dates):
for title in titles:
for poet in poets:
for date in dates:
return “The poem {} was published by {} in {}.”.format(title, poet, date)

Thank you very much

When that does print it will be reams of lines owing to the triple nested loops. This is one for the boneyard. Scrap it and think it through again.

Hint:

All the arrays are the same length. Establish a range of that size and iterate it for the index, then use it on all three objects in each iteration.

1 Like

im also confused in the last one:

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

for i in range(0,len(highlighted_poems_details)):

im actually confused alwaysi found this [0], [1] and then titles[i], poets[i], dates[i], i know its the index but when to know i actually have to used them?

and then to guess i needed two for loops, this is hard

Here’s my solution. My final for loop in particular feels clunky and brute forced. Any suggestions for cleaner code?

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

counter = 0
for i in range(len(titles)):
print("The poem " + titles[counter] + " was published by " + poets[counter] + "in " + dates[counter])
counter = counter + 1
``````
``````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"

#print(highlighted_poems)

highlighted_poems_list = highlighted_poems.split(',')

#print(highlighted_poems_list)

highlighted_poems_stripped = []

for poem in highlighted_poems_list:
temp = poem.strip(' ')
highlighted_poems_stripped.append(temp)

#print(highlighted_poems_stripped)

highlighted_poems_details = []

for record in highlighted_poems_stripped:
temp = record.split(':')
highlighted_poems_details.append(temp)

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

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

counter = 0
for i in range(len(titles)):
print("The poem " + titles[counter] + " was published by " + poets[counter] + "in " + dates[counter])
counter = counter + 1
``````

Can’t understand why this isn’t working for item #10.

The output produced is

My expectation was that it would iterate over the total range of the list Titles and that would output the other list variables at the respective range values. I printed the range values of the

Here is the relevant part of the code:

``````def poem_description(title, poet, date):
poem_desc = ''
for i in range(len(title)):
return poem_desc

print(poem_description(titles,dates,poets))

``````

Here is all the code:

``````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"

print(highlighted_poems)

highlighted_poems_list  = highlighted_poems.split(',')

print(highlighted_poems_list)

highlighted_poems_stripped = []

for x in highlighted_poems_list:
highlighted_poems_stripped.append(x.strip())

print(highlighted_poems_stripped)

highlighted_poems_details = []

for poem in highlighted_poems_stripped:
highlighted_poems_details.append(poem.split(':'))

print(highlighted_poems_details)

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

for i in highlighted_poems_details:
dates.append(i [-1])

for i in highlighted_poems_details:
titles.append(i [0])

for i in highlighted_poems_details:
poets.append(i [1])

print(range(len(titles)))
print(range(len(poets)))
print(range(len(dates)))

def poem_description(title, poet, date):
poem_desc = ''
for i in range(len(titles)):