FAQ: String Methods - Review

No, they haven’t taught us anything about f-strings or even mentioned them before. Why do they do this?

A little late to this topic but we can agree, the new string formatting came over the horizon rather later than these courses were written, or most of the them. The syntax wasn’t enabled until Python 3.6 was installed in the learning environment which is sand-boxed and may not include all the courses in that sandbox.

If you’ve had a chance to work with str.format() enough to be comfortable with the directives, then that will parlay over to f-string formatting fairly easily. There’s no great leap from one to the other and both (or rather all) formatting methods work in Python 3.6 and later.

So, f-string is newer then? Would you say that generally f-string is a better way to do things?

better is a subjective term. Once they were introduced I had no problem with liking them. It doesn’t matter as much as you getting the string representations you want, regardless which method. f-string is well thought out and it was only a matter of time until it would emerge. The nice thing is that it doesn’t call on built-in functions and lets print handle it internally.

1 Like

So, when I first saw the list of highlighted_poems, my initial instinct was to develop a list of lists …

highlighted_poems_list = highlighted_poems.split(",")

# clean-up highlighted_poems
poem_list = []
for each in highlighted_poems_list:
  poem_list.append(each.strip().split(":"))

and got what I expected …

# TEST: probe structure of poem_list (list of lists)
print(poem_list)

[['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']]

I was expecting that I could past each poem in the list and populate the output with the index of each needed field (name, title, year).

# TEST: each field is accessible
name = indexed_poems[1]
print(name)
title = indexed_poems[0]
print(title)
year = indexed_poems[2]
print(year)

like so …

poem_blurb = "The poem {title} was published by {poet} in {date}."
print(poem_blurb).format(poet=name, date=year, title=title)

Alas, no.

Can anyone provide some guidance? Is this even possible? Acceptable?

Remember poems_list is a list of lists. Iterate over that list, and extract each list in turn.

for x in poems_list:
    a, b, c = x    #  unpacking the list

If f-strings are supported,

print (f"The poem {b} was published by {a} in {c}")

otherwise what you have will do just fine.

1 Like

Thank you. Thank you. Thank you.

Yes, the unpacking was the part I was missing.

# itemize & populate fields: name, title, year
for item in range(len(poem_list)-1):
  title, author, year = poem_list[item]

# # TEST: each field is correctly accessed
#  print(title, author, year)

  poem_blurb = "The poem {title} was published by {author} in {year}.".format(title=title, author=author, year=year)
  print(poem_blurb)

It works beautifully.

1 Like

It’s not necessary to use the index since all you’re doing is looking up the element in the list.

for item in poems_list:
2 Likes

Right. Recently learned that too.
(and … title, author, year = item)

1 Like

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(’:’))**

print(highlighted_poems_details)

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

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

print(titles)
print(poets)
print(dates)

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?

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

ERROR:
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 = ,,