Codecademy Forums

FAQ: String Methods - Review

def poem_description(title, poet, date):
  poem_desc = ''
  for i in range(len(titles)): 
    poem_desc = "The poem {} was published by {} in {}".format(title[i],poet[i],date[i])
  return poem_desc

Every time around the loop, the variable, poem_desc is overwritten, so you only return the final instance of the loop. Collect them, somehow, perhaps in a list.

1 Like

Thanks. The below worked:

def poem_description(title, poet, date):
  poem_desc = []
  for i in range(len(titles)):
      poem_desc.append("The poem {} was published by {} in {}".format(title[i],poet[i],date[i]))
  return poem_desc

print(poem_description(titles,dates,poets))
1 Like

How does that code work for every index and we have just mentioned "0,1 ,2 " ? codeca

If we assume that highlighted_poems_details is a list of lists, then at each iteration, poem will be a list with three elements, hence indices 0, 1 and 2.

Help to understand please.
If i use list concantinashion (+=), not .append we get:

highlighted_poems_list = ['Afterimages:Audre Lorde:1997', '  The Shadow:William Carlos Williams:1915']

highlighted_poems_stripped = []
for item in highlighted_poems_list:
    highlighted_poems_stripped += item .strip()

print(highlighted_poems_stripped)

[β€˜A’, β€˜f’, β€˜t’, β€˜e’, β€˜r’, β€˜i’, β€˜m’, β€˜a’, β€˜g’, β€˜e’, β€˜s’, β€˜:’, β€˜A’, β€˜u’, β€˜d’, β€˜r’, β€˜e’, ’ ', β€˜L’, β€˜o’, β€˜r’, β€˜d’, β€˜e’, β€˜:’, β€˜1’, β€˜9’, β€˜9’, β€˜7’, β€˜T’, β€˜h’, β€˜e’, ’ ', β€˜S’, β€˜h’, β€˜a’, β€˜d’, β€˜o’, β€˜w’, β€˜:’, β€˜W’, β€˜i’, β€˜l’, β€˜l’, β€˜i’, β€˜a’, β€˜m’, ’ ', β€˜C’, β€˜a’, β€˜r’, β€˜l’, β€˜o’, β€˜s’, ’ ', β€˜W’, β€˜i’, β€˜l’, β€˜l’, β€˜i’, β€˜a’, β€˜m’, β€˜s’, β€˜:’, β€˜1’, β€˜9’, β€˜1’, β€˜5’]

But, item[0] in is not a single letter, item [0] is Afterimages:Audre Lorde:1997.

print(highlighted_poems_list[0])

Afterimages:Audre Lorde:1997

OR:

for item in highlighted_poems_list:
    print(item)

Afterimages:Audre Lorde:1997
The Shadow:William Carlos Williams:1915

So, if i use .append oll is good:

highlighted_poems_list = ['Afterimages:Audre Lorde:1997', '  The Shadow:William Carlos Williams:1915']

highlighted_poems_stripped = []
for i in highlighted_poems_list:
    highlighted_poems_stripped.append(i.strip())

print(highlighted_poems_stripped)

[β€˜Afterimages:Audre Lorde:1997’, β€˜The Shadow:William Carlos Williams:1915’]

Why in for loop we must use .append?

The += operator wants both of its operands to be of the same type, so if you say
my_list += my_string, it first does list(my_string), and then works like list.extend().

2 Likes

It should be noted this only applies to string lists, not number lists.

>>> a = ['a', 'b', 'c', 'd']
>>> a += 'e'
>>> a
['a', 'b', 'c', 'd', 'e']
>>> b = [1,2,3,4]
>>> b += 5
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> 
2 Likes

Yes, thanks; the right-hand object must be iterable, as with extend().

2 Likes

So,

>>> b += [5]
b += [5]
>>> b
[1, 2, 3, 4, 5]
>>> 
2 Likes