Trying to word out this code to better understand it

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

print(“The poem {} was published by {} in {}.”.format(titles[i], poets[i], dates[i]))

So this is the last function for one of the “Strings” lessons and I’m having trouble trying to reword what the function is actually doing to better understand why and how it works. I was wondering if somebody could explain what the function is doing?

Which bit is unfamiliar, if you mean the .format method it’s used for manipulating strings. You can look into the background if you like- https://docs.python.org/3/library/string.html#format-string-syntax

What you have is a string "The poem {} was published by {} in {}.". The curly braces are used as placeholders with standard formatting such that they are replaced by the positional arguments you use to the .format method.

Some very simple formatting examples-

print("{}".format(0))
Out: 0
print("My name is {}".format("Earl"))
Out: My name is Earl

The rules around this formatting are a little complex at first (it’s often referred to as a mini-language) but they make working with strings much easier. For general formatting the following is a goodresource:
https://pyformat.info/
Typically in newer versions of Python you’d now use f-string expressions instead, like f-Strings: RealPython but the formatting rules haven’t changed too much.

Thanks for your reply after looking at my question again, i believe my answer lies here:
for i in range(0,len(highlighted_poems_details)):
im still having trouble understanding what this part really means and how and when it should be used.

Python’s for loops step through iterators (objects that return items sequentially) rather than by index (this style x=0; list[x]; x++; list[x]; ...). Sometimes it may be preferable or necessary to use an index though. That’s what that line is intended for, you’ll get a range of indices (returned by iterator but that doesn’t really matter) that you can iterate through which is the same length as highlighted_poem_details.

With the use of that range you can access each element of highlighted_poem_details in sequence with highlighted_poems_details[i] because i changes on each iteration to index your list.

Try printing i on each iteration if you want to see how it works.

For i in range of 0 (starting point) to the length of highlighted_poems_details (ending point)
print(“The poem {} was published by {} in {}.”.format(titles[i], poets[i], dates[i]))

So the loop starts with the first element in the list (hightlighted_poems_details)
prints The poem titles[i] was published in poets[i] in dates[i]
then goes to the next element and prints
prints The poem titles[i] was published in poets[i] in dates[i]

The poem titles[i] was published in poets[i] in dates[i]

|
V

The poem titles[0] was published in poets[0] in dates[0]
The poem titles[1] was published in poets[1] in dates[1]

To however many elements there are in the list

Thank you both for your replies. They both helped tremendously with understanding how it works and how its worded out. I appreciate it!

1 Like