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

Eliel Afari-Brown
PRO
Eliel Afari-Brown
Pro Student 199 Points

I dont understand how to do the last question in for the join and split assignment

So for the last question for join and split objective it ask me to join the sundaes list while creating a new variable and for then adjust the menu variable so that the display_menu string is in the format placeholder instead of the previous sundaes one

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

3 Answers

Greg Kaleka
MOD
Greg Kaleka
Treehouse Moderator 36,749 Points

Hi Eliel,

You're SO CLOSE. The problem is that you're not using join() correctly. It's a little confusing because the instructions happen to put the ", " in parentheses just because they were an aside, but string joining works differently than in other languages. You just have to flip things around. It works like this:

'use this string to'.join(this_list_together)

So for this example:

', '.join(sundaes)

Cheers :beers:

-Greg

Valeshan Naidoo
Valeshan Naidoo
Full Stack JavaScript Techdegree Student 20,174 Points

Another thing I'd add on, is that you seem to be formatting the menu variable twice, once with the sundaes and the next with the display_menu. Since we're adding the sundaes inside of the display_menu, we can omit the first menu variable on line 3.

Diego Angarita
Diego Angarita
2,515 Points

I am having the same problem. I have spent hours on this question. I even checked my answer in the console and it seems to work fine. What am I getting wrong?

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

When I do print(display_menu) I get Our available flavors are: banana split, hot fudge, cherry, malted, black and white.

What am I doing wrong?

Greg Kaleka
Greg Kaleka
Treehouse Moderator 36,749 Points

Hey Diego - split() takes an argument for what character to split on. If you leave it blank, it defaults to space. That's not what you want; you'll end up with sundaes equal to this list:

['banana', 'split;hot', 'fudge;cherry;malted;black', 'and', 'white']

Make sure you pass split() the ";" character.

Also, I did not realize you could use format that way (on a variable that holds a string with brackets). It totally works, though! Thanks for teaching me something.

Diego Angarita
Diego Angarita
2,515 Points

Yea, I realized that I forgot that part when I was retyping everything from the console. The strange thing is that (with the correction) it works in the console but won't let me move past the quiz.

Greg Kaleka
Greg Kaleka
Treehouse Moderator 36,749 Points

OK I looked at the challenge, and the problem is you're not quite following the instructions. Give them another close read and make sure the "answer" is being assigned to the correct variable.

Always a good idea, if it seems like your code is "working" but you're not passing, to make sure you're actually doing what the challenge wants. Code that does what you think it should doesn't necessarily mean it's doing what the challenge is asking for.