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 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!