FAQ: String Methods - Joining Strings II

This community-built FAQ covers the “Joining Strings II” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise Joining Strings II

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I just completed the lesson and it says I passed, but my code appears to have accomplished nothing. Here is my code:

winter_trees_lines = ['All the complicated details', 'of the attiring and', 'the disattiring are completed!', 'A liquid moon', 'moves gently among', 'the long branches.', 'Thus having prepared their buds', 'against a sure winter', 'the wise trees', 'stand sleeping in the cold.']

winter_trees_full = "\n".join(winter_trees_lines)
print(winter_trees_lines)

Here is what prints to the screen:

['All the complicated details', 'of the attiring and', 'the disattiring are completed!', 'A liquid moon', 'moves gently among', 'the long branches.', 'Thus having prepared their buds', 'against a sure winter', 'the wise trees', 'stand sleeping in the cold.']

Why aren’t the strings being printed on separate lines?

because you are printing the original list. You should write

print(winter_trees_full)

then it will give desired result

All the complicated details
of the attiring and
the disattiring are completed!
A liquid moon
moves gently among
the long branches.
Thus having prepared their buds
against a sure winter
the wise trees
stand sleeping in the cold.

Hope it helps… :slightly_smiling_face:

Can I not start a new thread on this discussion?

So this only works when the existing delimiter in the list is a comma? I did…
walrus_w_commas = [“I am the egg man”, “They are the egg men”, “I am the walrus”, “Goo goo g'joob”]

walrus_new_line = “\n”.join(walrus_w_commas)

print(walrus_new_line)

which output exactly what I wanted: each lyric on a new line. But I didn’t specify to start a new line after each comma? If I change the commas to full-stops I get an error message so my question is, how does it know the delimiter is a comma?

Thanks

I have to append myself to this question even though it has not been answered. How does it know that the comma is the delimiter? I get everything else except why the comma is the one creating a new line. I understand \n creates a new line but why is it being done wherever there is a comma present in our list. Any help would be great.

1 Like

Hi Vicloop80

Our question was too silly to be answered! hahaha
I’m revisiting this material and I reset progress, redid the exercise, had the same problem and came back to the forum to find my original question. I think that’s what’s meant by a python loop! Did you eventually work it out? I don’t know the poem so I don’t know where the new lines begin. My question remains, how does it know to begin a new line at the comma when the delimeter is \n??

Seeing that this was never answered and it appears to be a decently common question from people on this exercise…

Comma is not the delimiter here. Delimiters exists within the string - so in this case the join is adding the new line delimiter to the list of strings you have, and thus you end up with a multi-line string where the previously individual strings are connected by the new line character (forming the new line).

I think the confusion here comes from the the comma within lists. Go through your previous exercises. Lists with strings are ALWAYS separated by commas. They don’t just float about. This is the same as a tuple of strings/variables/numbers are separated by commas. Comma is not the delimiter here. Delimiter only applies to string values.

How would you be able to differentiate one element to the next within a list without commas otherwise?