FAQ: String Methods - Review

I see, I understand.

for thread in thread_sold:
  for color in thread.split("&"):
    thread_sold_split.append(color)

What about the solution given by codeacademy? I thought it would return the individual letters (“w”, “h”, …) given the double for loop but it magically returns the whole words (white, blue, …)

How come if we append(color) it returns the whole words?

Getting drowsy… I cannot speak for CC authors. Their solutions are theirs, not mine. In the end, we think for ourselves.

Okok, sorry mtf for asking so many questions and many many thanks for your help. I really appreciate it thank you very much.

1 Like

i tried list comprehension in step 9 like this:
titles, poets, dates = [poem[0], poem[1], poem[2] for poem in highlighted_poems_details]
this didn’t work. is there a way how it would have worked?

The above implies a packed object containing three values since this is how we would unpack it.

z = [2, 8, 512]

a, b, c =  z

print (a)    # 2
print (b)    # 8
print (c)    # 512
2 Likes

thanks a lot for your answer! it made it more clear

1 Like

The Review mentions: "

.format() and f-strings allow you to interpolate a string with variables.

"
Was this implicitly explained because there was no lesson explaining f-strings unless I missed something.
Thanks!

2 Likes

Why doesn’t variable assignment in one line work?

highlighted_poems_details=[] titles=[] poets=[] dates=[]
Brings a syntax error

highlighted_poems_details=titles=poets=dates=[]
Also brings up a syntax error

Thanks

Can someone explain this code to me? I’m not sure why when we append those lists why we need to use the index numbers. Or where they even came from. I also don’t fully understand the second for statement. I am new to python, it’s only my second week coding. I’m sorry if I sound stupid.

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

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

We have composed a list for each poem. Each list has three elements that correspond to title, poet and date in that order. It means that each poem above in said list of three elements. The code extracts each element and appends to the corresponding list.

Python permits unpacking of list objects.

title, poet, date = poem
titles.append(title)
poets.append(poet)
dates.append(date)

which amounts to the same thing, without need of indexing. This doesn’t mean we toss the code you’ve shown. Both approaches are valid.

Ah I see. Thank you!

1 Like

Hi,

I typed:

for i in range(0,len(titles)):
return print(“The poem {title} was published by {poet} in {date}”.format(title, poet, date = titles[i], poets[i], dates[i]))

but I keep getting an error: “SyntaxError: positional argument follows keyword argument”

Could somebody please explain what that means?

Thank you !

As far as I can tell, .format does not support unpacking so the variables do not get assigned as you might expect. Each needs to be directly declared.

.format(title = titles[i], poet = poets[i], date = dates[i])
1 Like

Makes sense! Thank you so much!

1 Like

Nope, you’re right, this wasn’t explained.

1 Like

please what’s wrong with this code, i got Error : SyntaxError: ‘return’ outside function

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”

highlighted_poems_list = highlighted_poems.split(’,’)

highlighted_poems_stripped =

for poem in highlighted_poems_list:
highlighted_poems_stripped.append(poem.strip())

highlighted_poems_details =

for poem in highlighted_poems_stripped:
highlighted_poems_details.append(poem.split(’:’))

print(highlighted_poems_details)
titles =
poets =
dates =

for poem in highlighted_poems_details:
titles.append(poem[0])
poets.append(poem[1])
dates.append(poem[2])
for poem in highlighted_poems_details:
msg = “The poem {titles[0]} was punlished by {poets[0]} in {dates[0]}”
return msg

1 Like

Return cannot be used outside a function. You don’t need to return anything from a loop. It’s just a quick way to repeat statements of code.
If you’re submitting a query on code to the forums pease try to format it. It makes things easier to debug. You can format code using a triple backquote/accent grave character `
For example…
` ` `
x = 3
` ` `
becomes

x = 3

Happy coding!

3 Likes

Hello, I am interested in understanding “f-strings” aswell!

By and large you can use them like the .format() method you have come across but with the names you are referencing inside the { }.

x = 3
lst = [0, 5, 6]
print(f"x={x}, the object named lst prints {lst} and 3 * 5 = {3*5}")

This is only a very very basic overview, for more info see the docs or there are a number of decent guides you can find with a quick search-
https://docs.python.org/3/tutorial/inputoutput.html#formatted-string-literals

2 Likes

Man I also have same problem. I am very new too and also did not quite understand the loop series. although I understood this problem pretty much. You should check some youtube vid for each series. That may help.