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

Lee Ashton
Lee Ashton
889 Points

splitting and assigning strings to new variable. Help me please, was sailing along so well.

getting stuck on splitting strings and assigning them to new variable

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

3 Answers

split() splits a string with the delimiter provided.

So,

"some; string; is; here".split(';')

will return a list containing

["some", " string", " is", " here"].

maybe you meant something like this:

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

"sundaes: {}".format(available.split(';'))

The answer above answers the way split() is to be used.

here is the code with explanation:

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

#task-1
#split() function splits the string using the delimiter provided and returns a list.
#for example
#"someohhoohhostringohhoohhoisohhoohhohere".split('ohhoohho')
#will return ['some', 'string', 'is', 'here']

#here we have to: 'Use .split() to break the available string apart on the semi-colons (;).'
#so, it would be...  available.split(';')
# Assign this to a new variable sundaes.
#and that would be... sundaes = available.split(';')

sundaes = available.split(';')

#task-2
#Make a new variable named menu that's set to "Our available flavors are: {}.".
menu = "Our available flavors are: {}."

#task-3
#join() function joins the element of the list using the string provided as joiner.
#it is to be remembered that split and join are functions of a String (not list)
#for example: "ACHUUACHUU".join(['some', 'string', 'is', 'here'])
#will return: 'someACHUUACHUUstringACHUUACHUUisACHUUACHUUhere'
#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 (", ").
display_menu = ", ".join(sundaes)
#Then reassign the menu variable to use the existing variable and .format() to replace the placeholder with the new string in display_menu.
menu = menu.format(display_menu)

Hope this answers your query.

Lee Ashton
Lee Ashton
889 Points

Thanks for responding, I typed the same code as you have and it keeps telling me to try again. And I have tried various ways of typing the code different but still no joy.

Genaro Cedillo
PLUS
Genaro Cedillo
Courses Plus Student 471 Points

sundaes = available.split(';') is the answer to this its then next part i have trouble too