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

Seemed like a bit of a merry go round here...

"available " is a string that is converted to a list "sundaes", that is converted back to a string "string". Was this done just to change the ";" to ","? Is there a more straight forward way to do this?

# string
available = "banana split;hot fudge;cherry;malted;black and white"
# string to a list
sundaes = available.split(";")
# new string with an empty var
menu = "Our available flavors are: {}."
# back to a list
string = ', '.join(sundaes)
# string of available flavors goes into the menu var
menu = menu.format(string)

3 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there john larson. As you suspected there is a slightly more direct way this can be accomplished. But I think the idea here is to make sure that you can do this so that you're set up to work with things like CSV files later down the road. I could be wrong, but it's the theory I have regarding this particular challenge. :smiley:

I whipped up this code that will do the same thing, but in a little more direct way as you put it.

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

edited for additional code Woops forgot the first part of the menu string! Sorry.

Hope this helps! :sparkles:

Chancellor Griffin
Chancellor Griffin
2,212 Points

Hey, I recently had trouble with this challenge as well because I think the promt is a tad confusing. after task 2 I think it asks you to make that menu variable again and join it, like your line looks like here:

# back to a list
string = ', '.join(sundaes)

this can actually just be put at the end of the line. I had the same problem in my lab

# the menu variable should look like:
menu = "Our available flavors are: {}.".format(", ".join(sundaes))

and also, like I was saying before, the extra menu variable isn't needed, although the promt did mention it.

menu = menu.format(string) # not needed

So you can format using join!

For anyone who had the same logic but is still failing:

I noticed that if you put the ", " string in a separate variable (e.g. separator = ", ") you will fail. You have to format it like John Larson did to pass:

string = ', '.join(sundaes)

That's kinda cool...I never thought about putting something like a separator in a variable, thanks