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()

using .format and .join

Have been trying to solve task 3 in this split/join problem for an hour. have tried multiple iterations. Cant do it.

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

2 Answers

So we have:

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes=available.split(";")

sundaes now contains all the elements within available which were seperated by a ";".

In task 3 you are asked to make a variable display_menu which contains all these items seperated by a ", ". An easy way of doing this in python is by using the join function. We first define with what string we want to join each element from the list, then we place a dot and write the funtion name which is join: ", ".join() we pass the array to the function: ", ".join(sundaes) and assign it to the variable display_menu: display_menu = ", ".join(sundaes). Now display_menu contains a string with all the elements from sundaes seperated by a ", ".

Now all we need to do is fill in display_menu into the {} of menu which we defined in task 2. An easy way of doing this in python is using the .format() function. We first write the string we want to format, which in our case is "Our available flavors are: {}.", then we write: "Our available flavors are: {}.".format(display_menu). Finally we assign this to the menu variable: menu = "Our available flavors are: {}.".format(display_menu). And that's it.

The complete code should look like this:

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes=available.split(";")

display_menu= ", ".join(sundaes)
menu = "Our available flavors are: {}".format(display_menu)

thanks. drove me crazy