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 trialNick Evershed
6,429 Points["".join(rainbow[-6:])]
I don't understand this, it confused me so much
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsBreaking it down:
# the last 6 elements of the list rainbow
rainbow[-6:]
# "".join() creates a string using these elements.
# "" means the separation between elements is the empty string, that is, no space
"".join(rainbow[-6:])
# wrapping the expression in [ ] creates a list with the joined string as the only item
["".join(rainbow[-6:])]
Creating a string from a list of items can seem confusing. A built-in method on strings is the .join(some_list)
method. It says
"use this string as a connecting segment to create a new string using each of the items in
some_list"
An example:
>>> a = ['1', '2', '3', '4', '5', '6']
>>> "---".join(a)
'1---2---3---4---5---6'
>>> "-".join(a)
'1-2-3-4-5-6'
>>> "".join(a)
'123456'
Post back if you have any more questions. Good luck!!
Nick Evershed
6,429 PointsI guess its the quotation marks that really throw me
Chris Freeman
Treehouse Moderator 68,457 PointsAnswer expanded above to include more .join()
examples.
Nick Evershed
6,429 PointsNick Evershed
6,429 PointsThanks so much, I didn't realise what u needed to join with came before .join, well now I understand this but a lot of the stuff in my stage is actually really confusing me, but I don't want to start the course all the way from the beginning, I don't know what to do
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsSometimes repeating content can help to reinforce what you know and help learn the concepts that need a better understanding. Feel free to post more questions to the community forum if you get stuck again.