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

Margo Oshry
Margo Oshry
697 Points

How do i combine a list into a new string joining them with (",") and then using that string with a .format to do more?

How do i combine a list into a new string joining them with (",") and then using that string with a .format to then add that to an existing variable and changing the end value of that existing variable?

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

2 Answers

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

sundaes = available.split(";")

menu = "Our available flavors are: {}".format(", ".join(sundaes))
Sahil Sharma
Sahil Sharma
4,791 Points
available = "banana split;hot fudge;cherry;malted;black and white"
available = available.split(";")
menu= "Our available flavors are: {}".format(" , ".join(available))

Notice that I did not introduce the variable sundaes, in your code while using .join() the argument in its parenthesis is a string i.e. available .join() works differently for lists and strings, it joins all the elements of a list using the character that you state in this case " ," a comma in case of strings however it treats every letter/character as an element so it won't give the desired output. For instance: list = ["ab", "bc"] string = "abc" Using ", ".join(list) will give ab, bc while ", ".join(string) will give a, b, c