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

spliting and joining

i have made the variables below

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

sundaes = available.split(';')

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

how can i Combine the sundaes list into a new string, joining them with a comma and a space (", ").

Use that string with a .format() on our existing menu variable and replace the current value of menu

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

4 Answers

Thank you guys. That was helpful.

Thats the excactly method because i wanted the menu variable to be a string not a list.. because after Spliting they are no semicolons but the variable is still a List so our first thing was to ...... Point 1. Combine the sundaes LIST into a new STRING, JOINING them with a comma and a space (", "). which you taught me with a ", ".join

because i had already have the MENU variable (if you look at my last line on my initial code) The second thing was to Point 2.Use that string that we JOINED in Point1 a with a .format() on our existing menu variable and replace the current value of menu which again you taught me with a .format()

forgive me i am still a Newbie in Python so my question wasn't very clear but thank you very much your solution was excellent

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Terrence,

cool that I could help !!!

Shout out here in the forum if you need any help.

See you :)

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Terrence,

yoa are almost done :)

you can do it this way:

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

menu = "Our available flavors are: {}.".format(", ".join(sundaes))
# the format method takes the String element ", "
# that you use for joining the sundaes variable

Grigorij

Hi Grigorij

I have done that too But this section of the question "how can i Combine the sundaes list into a new string, joining them with a comma and a space (", "). Use that string with a .format() on our existing menu variable and replace the current value of menu" is troubling me. I have those variables already from stage 1 and 2, the last stage eish, it has confused me!

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi,

I am not sure I understand your question correctly but:

After splitting the sundaes variable it looks like this:

>>> available = "banana split;hot fudge;cherry;malted;black and white"
>>> sundaes = available.split(";")
>>> sundaes
['banana split', 'hot fudge', 'cherry', 'malted', 'black and white']
>>> 

You see no semicolons anymore.

Then you can place this new text (without semicolons) on position {} inside your menu using the format method and two curly brackets.

To add a new text element like ', ' use the join method as argument inside format method.

The result is this:

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

Makes more sense?

Grigorij