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 (Retired) Shopping List Lists and Strings

Splitting my name to create a list?

I'm asked to created a variable with my full name in it. Then to create a list using my name as the individual list items? I think I got it but I'm not allowed to move on. What gives?

greeting_list.py
full_name = "Ben Muresan"
name_list = []

name_list.append (full_name.split( ))

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher
full_name = "Ben Muresan"
name_list = []
name_list.append(full_name.split(" "))

So what happens when you do full_name.split(" ") (which, since we want to split on the whitespace, we can leave out the argument all together)? Python makes a new list that looks like ["Ben", "Muresan"]. Then it appends that list to your name_list variable. So at the end, name_list is [["Ben", "Muresan"]]. Instead of name_list being a list of the pieces of your name, it's now a list with a list of the pieces of your name in it.

Have you tried creating name_list by using .split()? Cut out the middleman.

Hi Ben!

Think about what parameter you might need to put into the .split() function. If you are trying to split your name into two separate items how would you split it? Have a think and reply back if you can't get the answer!

-Luke

I've spent over an hour on this exercise. Ive added spaces, commas, whatever I could think of to the .split() function. I know the name needs to be split on the space. Nothing.

If it needs to be split on the space maybe try the line of code below.

.split(" ")

I swear I did at one point. But I'll try again. Thank you.

Alright, tell me if it works out for you!

full_name = "Ben Muresan" name_list = []

name_list.append (full_name.split(" "))

Doesn't work. Any ideas?

Oh, I have spotted the problem! Look at my code below!

full_name = "Luke Glazebrook"
name_list = full_name.split(" ")

I see let me try it, but to be a list doesn't it need to be in [] brackets?