FAQ: String Methods - Review

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

There is your NoneType. The closing parenthesis should be after .format().

1 Like

Thanks, must have dyslexia’d out and missed that in the solution. Coming from functional programming, R and some ancient languages so my syntax is a train wreck too.

Works as intended now.

1 Like

why am i getting a systax error?




Missing closing parenthesis.

While I was doing this review, I saw a problem that I wanted to address. I noticed when we did step 7, it created a list of embedded lists. And one thing I became curious of was how (if I wanted) I would take one embedded list that has 3 separate elements and combine them into one element. I worked for about 30 solid minutes to figure out how. Here’s my example.

This example shows how to take an embedded list that contains multiple elements and join the elements together to make 1 single element within the embedded list,

old_list = ["Iron Man", "Captain America", "The Hulk", "Thor, the god of thunder"]

new_list = []
for i in old_list:
newest_list = []
for i in range(len(new_list)):
    newest_list.append([' '.join(new_list[i][:len(new_list[i])])])


[['Iron Man'], ['Captain America'], ['The Hulk'], ['Thor, the god of thunder']]

1 Like

I am getting this error. I have reported the bug.

I have tested it on Firefox, edge , and chrome. All show the same result.

@mtf, @victoria-dr : What do you think?

Please post a link to the exercise in question.

I’m not sure what I am missing here. When I check my code it says it didn’t return the expected value, but I compared it manually to the list of poems and everything is there and in the right order. Can someone point me in the right direction?

We would truly benefit from having a link to the exercise/project. Please post in a reply.