Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Python Data Types Use .split() and .join()

what is wrong with is code?

available = "banana split;hot fudge;cherry;malted;black and white" sundaes = available.split(";") menu = "Our available flavors are: {}." display_menu = sundaes.join(",") "Our avaialable flavors: {}.".format("display_menu")

banana.py
available = "banana split;hot fudge;cherry;malted;black and white"
sundaes = available.split(";")
menu = "Our available flavors are: {}."
display_menu = sundaes.join(",")
"Our avaialable flavors: {}.".format("display_menu")

keeps saying Oops! It looks like Task 1 is no longer passing.

1 Answer

It is important to remember that "list" object has no attribute or function called 'join'. Join works on strings.

Output of "join" is a string.

what join does is, it takes an argument and insert a string between its each first-level iterative entity. Lets see some example to make sense of it.

list1 = ['a', 'b', 'c']
list2= ["xxxx", "yyy", "zz", "a"]
string1 = "TYU"
string2 = "123"
#now lets see join coming into picture

list1.join(string1)
#It will give error
string1.join(list1)
#it will work fine and the output will be: aTYUbTYUcTYUd

#can you guess the output of:
string1.join(string2)
#the output will be: 1TYU2TYU3

#But since string is also iterable(as seen in string1.join()string2 ) so what you think is the output of
string2.join(list2);
#run it in a workspace and you will see the meaning "first-level iterative entity" in last sentence
#the output will be: xxxx123yyy123zz123a and NOT x123x123x123x123y123y123y123z123z123a

so there was the one mistake you were doing. Second is,

.format(display_menu)
#its should not be in ""

so finally

available = "banana split;hot fudge;cherry;malted;black and white"

sundaes = available.split(';')

menu = "Our available flavors are: {}."

display_menu = ", ".join(sundaes)

menu = menu.format(display_menu)

Hope this helped.

Great explanation! Thank you very much!