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

What am I doing wrong?

So I type this into eclipse and print menu and it looks just fine. Although this is basically exactly what Kennith did in the video, maybe he (or whoever writes these) wants me to do something different? Also, what does .format() being highlighted in pink mean?


1 Answer

Nathan Tallack
Nathan Tallack
22,159 Points

Yeah, this one was a little confusing. I am sure the ugly way that I did it was not the best way either, but it worked.

Consider my code below.

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

sundaes = available.split(";")

menu = "Our available flavors are: {}."

menu = menu.format(", ".join(sundaes))

First we make the sundaes variable which is a list of strings made up of all the words split on the ; character.

Then we make a variable named menu which is our string with the braces placeholder in it to use with the format method later.

Then we overwrite our menu variable with the string formatted (so that means what is inside the format arguement will be placed where the braces are) and that argument is the result of the join method.

Join is a little confusing for me. But I worked out that you call join on a string that you want to be between the things you are joining. In this case we are putting a commer and a space so the string ", " is what we are calling .join on and I pass in the sundaes list to that.

So the end result would be the big long string for menu which reads:

"Our available flavors are: banana split, hot fudge, cherry, malted, black and white."

Again, ugly but functional. :)

Okay. That makes sense. I wasn't making the second variable named menu and overwriting it. Now I also know we can overwrite variables like that. Thankyou!

Nathan Tallack
Nathan Tallack
22,159 Points

Yeah. Later you will learn about scope and you will learn sometimes you are not overwriting that original variable when you look like you are but making another copy of it.

But for now you can just understand that you are overwriting it.

Kind of like when your variable is an integer and you increase it by 1.

a = 1
a = a + 1  # Now a = 2