FAQ: String Methods - Review

When I run my code in Sublime, everything works seemingly perfectly. Titles, authors, and dates all store and print the proper information. However, whenever I run the code in Codeacademy’s executor, I get this error on task 6:

I don’t understand what the error message means to my code, and any help would be appreciated.

Here is all of my code in Sublime:
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 :
** highlighted_poems_stripped.append(poem.strip())**

# print(highlighted_poems_stripped)

highlighted_poems_details = []
for publishment in highlighted_poems_stripped :
** highlighted_poems_details.append(publishment.split(’:’))**


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

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


Might be you have jumped ahead. Remove the lines that follow the declaration of the empty list and try that.

Aside (lightheartedly)

One does realize that an executor is the person tasked with carrying out or overseeing carried out the instructions in a deceased person’s last will and testament, eh?

1 Like

I am embarrassed to say that worked. I really should have tried that beforehand, but I figured it wouldn’t affect the task. Thank you so much!

1 Like

I got excited when f-strings were mentioned in the Review section. I see there is A LOT of discussion about this but I just wanted to share that I figured out how to write one on my own by reading the Python documentation (link to Python docs). Something I vastly underestimated when I first started to teach myself coding etc. was that you have look up things on your own and that ability to find the right information, consume it, and apply it, not only makes you a stronger coder/engineer/dev but is commonly part of the job. At first this seemed impossible because there is so so so much info out there but after a few months of trying, its become less daunting.

Could someone help me figure out why this code for assigning each variable wouldn’t work?

titles, poets, dates = [[i[0],i[1],i[2]] for i in highlighted_poems_details]

titles, poets, dates = [[i[0],i[1],i[2]] for i in highlighted_poems_details]
ValueError: too many values to unpack (expected 3)

Thank you!

Please have a look at the following FAQ, especially the section on formatting code for the forums.

You need to consider the shape of the list you output here, if you’re not sure try printing the output so you know what changes you have made.

For each element i in highlight_poems_details you create a new list consisting of the first three elements of i. In this case what you end up doing is putting the exact same list of lists back together-

[[i[0],i[1],i[2]] for i in highlighted_poems_details] == highlighted_poems_details

So the len of your new list is still 11 and you cannot unpack it into three variables. Working with for loops and append might be your easiest option here. For an alternative you can look into how to unpack with zip but note that the instructions ask for titles, poets and dates to be lists so you need to be careful with the output.

1 Like

Thank you for this helpful support. I will look into the sources you pointed out to me and go over some examples. Thanks also for the formatting guideline. Have a happy holiday!

1 Like

[Codecademy_string_methods.py · GitHub]

Guys here is the link for string_method exercise…

try titles,poets,dates = ,,

1 Like

for the last step? can someone explain why we use range here? Is the title, poet, and date related to lists we created in the previous step?

As individual entries of titles, poets and dates, respectively, yes. We don’t need to iterate over any of the lists, only a range of that size.

n = len(titles)
for i in range(n):
    title, poet, date = titles[i], poets[i], dates[i]
    print(f"{title} - {poet} - {date}")
1 Like

Thank you for the clarification

1 Like

For my last for loop, do I HAVE to use the range() function in order to loop through the list length?

I am coming from Javascript and thought if its possible to have a similar loop syntax.

initialize i with i=0 , defines a condition like i <= len(variable) and increment i++ :thinking:

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) #print() highlighted_poems_list = highlighted_poems.split(',') #comma is delimiter #print(highlighted_poems_list) #print() highlighted_poems_stripped = [] for poem in highlighted_poems_list: highlighted_poems_stripped.append(poem.strip()) #print(highlighted_poems_stripped) highlighted_poems_details = [] for info in highlighted_poems_stripped: highlighted_poems_details.append(info.split(':')) #print(highlighted_poems_details) dates = [] titles = [] poets = [] for details in highlighted_poems_details: dates.append(details[2]) titles.append(details[0].upper()) poets.append(details[1].title()) for i in range(0,len(highlighted_poems_details)): message = "The {} poem '{}' was written by {}." x = message.format(dates[i], titles[i], poets[i]) print(x)

From what you’ve described I think the answer is not in the way you want, or at least not in a single line.

However, if you look at the range type itself it takes 3 arguments for initialisation (start, stop, step) in a not dissimilar way to the loop your described. So if it helps you to think of it this way then you could simply map the same steps to those three values for now-

for (i = 0 ; i < len(variable); i++)
for i in range(0, len(variable), 1)

Sorry for the C like looping style I’m unfamiliar with the norm for javascript.

Bear in mind that range is used simply because it’s convenient and efficient. You could equally pre-define the values you want to step through.

Python’s for loops are generally best at iterating through the items in a collection without an index (I think the closest javascript equivalent is something like for-of loops but I don’t know it well enough to say that with any certainty).

Python does not have C like (JavaScript, Java, &c.) loops.

for element in iterable:

The JavaScript equivalent of this is,

for (let element of iterable) {


In Python there are several forms of iterables:

str => character string

list => ordered and mutable array-like structure

tuple => ordered and non-mutable array-like structure

set => non-ordered, mutable array-like structure

dict => ordered, mutable associative array-like structure

Note: dict is unordered previous to verson 3.6

The use of iterators can reduce the footprint in memory. A range() is a type of iterator, with the exception that it is not consumed when we iterate it, unlike most other iterators since this is a common trait, and part of the definition of iterator.

We can iterate a range without casting it as a list, but we can only mutate it once we cast it to a list (retains order) or set (order may be lost).

We don’t have to recast a range to store and re-use it.

>>> a = range(10)
>>> for x in a:
	print (x)


Thank you for the explanation. @tgrtim

I’m in the early stages of Python but there seems to be quite a lot of similarities (pattern wise) with Javascript and I thought that there would be a possibility here with loops that’s worth exploring.

I’m shifting my learning strategy by focusing on language patterns more so than remembering built in functions. I understand I have to memorize a couple for each language/library but really don’t want to rack up the list…no pun intended (if there is one…).

Thank you for the explanation. I have not gotten to tuple, set, and dic lessons but looking forward to it. 'll do a high level study on the range function. At the end of the day, I’m all for reduction of foot print on memory.

Hi, I’ve just done this exercise and am stuck on 10

This is the code I entered:

and it claimed it was correct. However reading this and looking at the solution I don’t understand how mine is correct? Could someone explain it to me, as I have not included indices or range etc!

Hard to tell why it is correct when there are misnamed parameters on line 33.

Working through this section and hit a…curious problem.

I got the code “working” in that the checkmark fired, but don’t get the expected output. Code is below:

for entry in highlighted_poems_details:

for i in range(0, len(highlighted_poems_details)):
  print("The poem {} was published by {} in {}").format(titles[i], poets[i], dates[i])

Which bar variable names matches the model answer on inspection, however the output is:

The poem {} was published by {} in {}
Traceback (most recent call last):
File “script.py”, line 31, in
print(“The poem {} was published by {} in {}”).format(titles[i], poets[i], dates[i])
AttributeError: ‘NoneType’ object has no attribute ‘format’

I’m moving on tonight just to keep momentum but want to keep an eye on this as I’ve been debugging for some time and can’t see what’s happening. Any ideas?


  • Willow