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

Maryan Tuzyak
Maryan Tuzyak
1,495 Points

.join() .split() stuck on 3rd section of quiz dont understand it

Alright, let's finish making our menu. Combine the sundaes list into a new variable named display_menu, where each item in the list is rejoined together by a comma and a space (", "). Then reassign the menu variable to use the existing variable and .format() to replace the placeholder with the new string in display_menu. i joined sundaes into new variable by the ",". i dont know what it means reassign menu variable to existing one

banana.py
available = "banana split;hot fudge;cherry;malted;black and white"
available.split(';')
sundaes = available.split(';')
menu = "our available flavors are: {}."
display_menu = sundaes", ".join(",")
menu = "our available flavors are:{}".format(", ".join(display_menu))
Péter Juhász
Péter Juhász
4,768 Points

I can't remember this task, but I assume you should produce the following result: "our available flavors are: banana split, hot fudge, cherry, malted, black and white" but not with replace ";" with ", " but split the string into list and rejoinining the list into string again.

some notes: available.split(';') # this line has no meaning. the python does the split, but you need the result so you should put it into a variable

sundaes = available.split(';') # this is ok; sundaes is a list: ['banana split', 'hot fudge', 'cherry', 'malted', 'black and white']

menu = "our available flavors are: {}." # you can do that but you did not used menu

you can replace the last line display_menu = menu.format(", ".join(sundaes)) which is equivalent display_menu = "our available flavors are: {}.".format(", ".join(sundaes))

display_menu = sundaes", ".join(",") # has no meaning sundaes is a list, and you wrote a string directly after it ", ". Python cannot understand this. You can generally do something with two string, two lists; etc. not with different types. If you have two different types you have to convert one of them.

remember: split: makes a list from string join: makes a string from list

1 Answer

Cameron Nilon
Cameron Nilon
15,487 Points

Hey Maryan,

You are almost there. To answer your question on reassigning, basically think of it as overwriting an existing variable with a new value.

Now for you code challenge.

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) #Note this is reassigning your menu variable with a new value