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 trialBen Muresan
265 PointsSplitting 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?
full_name = "Ben Muresan"
name_list = []
name_list.append (full_name.split( ))
2 Answers
Kenneth Love
Treehouse Guest Teacherfull_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.
Luke Glazebrook
13,564 PointsHi 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
Ben Muresan
265 PointsI'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.
Luke Glazebrook
13,564 PointsIf it needs to be split on the space maybe try the line of code below.
.split(" ")
Ben Muresan
265 PointsI swear I did at one point. But I'll try again. Thank you.
Luke Glazebrook
13,564 PointsAlright, tell me if it works out for you!
Ben Muresan
265 Pointsfull_name = "Ben Muresan" name_list = []
name_list.append (full_name.split(" "))
Doesn't work. Any ideas?
Luke Glazebrook
13,564 PointsOh, I have spotted the problem! Look at my code below!
full_name = "Luke Glazebrook"
name_list = full_name.split(" ")
Ben Muresan
265 PointsI see let me try it, but to be a list doesn't it need to be in [] brackets?