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

Kyle Hille
Kyle Hille
720 Points

Use .split() and .join()

I finished this 'mission' but I didn't use everything it told me to.

I ended up not using display_menu at all, I just put it into menu (which the post says you can do if you are feeling really cheeky), but I would like someone to be able to show me or simply just email me about how to get it to work with the display_menu? To me personally it feels a bit redundant, and with that extra code, I feel like something would get lost or it would be that much more to go through and try to debug?

Thanks in advance

5 Answers

Go through the comments for explanations:

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

#Use Split to break: and it returns a list
sundaes = available.split(";");

#creating a menu as told
menu = "Our available flavors are: {}.";

#Join all the List-items using the string=", "  resulting into a final string containing all the list items separated by 'join' string
display_menu = ", ".join(sundaes);

#finally use the format function 
menu = menu.format(display_menu)
Kyle Hille
Kyle Hille
720 Points

I guess you could write it like that? Do you know of any benefit of doing it that way instead of :

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

also, are all these semicolons needed? I haven't used them so far and all my code seems to compile? also,also how did you get python to show? I apologize for my plain text.

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

Not any particular benefit of doing it that way. In our case menu was already a string that was declared before using format function. And what you have done is combined many tasks into one and a new user might not be able to understand all of that in a single line of code.

To add python add three backtics(`) followed by keyword "python" and close the code by three backtics. See Markdown Cheatsheet (when you try to comment or answer) for more details.

About semi-colons: Its not needed in Python at all. But it helps me to identify where my statement ended, so i considered it to be a good practice. There are many other programmers who do not like to add semi-colons.

But its more of a choice. Either way they do not harm your code in anyway. Also, i came from C/C++ background so needless to say, its a habit now. =D .

Also, I would encourage to close and answer by upvoting an answer(only if it satisfies your query). If an answer is not being accepted for a question then the question is considered to be a live question(not being correctly answered) and people might answer it again and again. Although it doesn't seems to be an celebrated practice here but you'll see on major forums this is the understanding of the seekers and the people who answers.

Enjoy coding!!!

Kyle Hille
Kyle Hille
720 Points

Thankkkkkkks. I also have done some C++, so I was wondering about it! Upvoted and answered! <3

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

While the semicolons won't stop the code from compiling and running (usually), don't use them when writing code for other Python programmers. Your code will stand out and you'll likely be asked to remove them every time.

Kyle Hille
Kyle Hille
720 Points

Is there a reason why you would be asked to remove them? Simply for some unwritten law?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

They're unnecessary in Python and make the code stand out (in a bad way). Ideally, all Python code looks the same and you can't tell who wrote any individual line.

Kyle Hille
Kyle Hille
720 Points

Hmmm, I guess that makes sense. The first thing that comes to my mind is that if you were able to 'follow' who was writing what code, maybe you could leave yourself open to penetration tests, if they were wise enough to find common flaws in that programmers' code?

Thinking waaaaaaaayyyyyyyyyyyyyyyyyyyyyyy out of the box there lol

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hahaha, I mean, I guess that's possible but I'd think very unlikely. Really, though, it's just that code should be uniform. You don't want to be reading through a large code base and be like "what the heck? where did that come from?!"

Kyle Hille
Kyle Hille
720 Points

Haha, I understand! Thanks! We simply just want to remove it, because if it's not there, there is less chance for confusion.

and for penetration testers.....