Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introducing Lists Using Lists Split and Join

Don't understand why the comma doesn't show up in the front of "to_line", even though it is written to

to_line = ", ".join(attendees) cc_line = ", ".join(optional_attendees) print("To: " + to_line) print("CC: " + cc_line)

I understand it all, except I don't understand why the comma doesn't show up in the beginning despite the code stating that it should.

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Not sure I understand your question, what code is written to put a comma at the beginning of the "to_line"?

3 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Ah, OK - I think you're just a little confused with what the join() does.

Here's the manual definition: str.join(iterable) Return a string which is the concatenation of the strings in iterable. A TypeError will be raised if there are any non-string values in iterable, including bytes objects. The separator between elements is the string providing this method.

The comma space at the beginning is just the separator which is placed between the concatenated values of the iterable (list, str, tuple...). I think that's where you getting confused???

Try some simple code like below in your REPL:

a = (', ').join('this is a string')
print(a)
# output is
t, h, i, s,  , i, s,  , a,  , s, t, r, i, n, g
# or try a list
a = (' xxx ').join(['this', 'is', 'a', 'list', 'of', '7', 'strings'])
print(a)
# output
this xxx is xxx a xxx list xxx of xxx 7 xxx strings

So the code says to join the attendees list to the comma. But when returned the attendees list comes before the comma. I just found that a little confusing. Does it mean that the join rule makes the object you wish to join, join before the target. I hope this makes sense, it is kind of confusing to explain

Ahh I get it now, thank you!