Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!

Victor Warner
Victor Warner
2,830 Points

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
210,850 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
210,850 Points

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

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

Steven Parker
Steven Parker
210,850 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
210,850 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
210,850 Points

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