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 Functions, Packing, and Unpacking Packing and Unpacking Unpacking, a Practical Example

How does one accommodate for last names with a space in them?

How does one accommodate for last names with a space in them in a scenario like this? For example, the last name "De Longhi"? Thanks!

How come at maxsplit = 3 I get an error for too many values to unpack ( expected 2)

Enter full name: Keanu Rea ves

4 Answers

Steven Parker
Steven Parker
231,269 Points

The "split" method takes an optional keyword argument to limit how many splits are performed:

first, last = input("Enter your full name: \n").split(' ', maxsplit=1)

Thanks!

Steven Parker
Steven Parker
231,269 Points

Glad I could help, and happy holidays! :christmas_tree:

And if the name was something like "Anne Marie Lewis?"

Steven Parker
Steven Parker
231,269 Points

You might need to ask the user to enter the name a certain way and program for that (like last name first with a comma). Otherwise, how could you tell if it's the first or last that has 2 words?

My sister-in-law would not like that. This is why separate boxes for first and last names works best. But "split" is a neat trick - just not for this

Steven Parker
Steven Parker
231,269 Points

Separate boxes is another way to place responsibility for making the distinction on the user.

There are many times when the user would prefer to take responsibility for designating their family and given names. Take some of my friends: Chang Woo Hee, Horvat Andras, Anna Lopez Gonzales, - or even myself when I use all four names in my official legal name.

Steven Parker
Steven Parker
231,269 Points

That is a good example of a UX design decision. But then that's a separate course series. :wink: