tags:

views:

2196

answers:

14

ok so basically I am asking the question of their name I want this to be one input rather than Forename and Surname.

Now is there any way of splitting this name? and taking just the last word from the "Sentence" e.g.

name = "Thomas Winter"
print name.split()

and what would be output is just "Winter"

+1  A: 

Like this:

print name.split()[-1]
JesperE
print name.split()[-1]notice the ()
Cipher
Sorry. I'm a little Ruby-damaged.
JesperE
A: 

Hi Nick, you'd probably want to use rsplit for this:

rsplit([sep [,maxsplit]])

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done, the rightmost ones. If sep is not specified or None, any whitespace string is a separator. Except for splitting from the right, rsplit() behaves like split() which is described in detail below. New in version 2.4.

Adam Alexander
+12  A: 

The problem with trying to split the names from a single input is that you won't get the full surname for people with spaces in their surname, and I don't believe you'll be able to write code to manage that completely.

I would recommend that you ask for the names separately if it is at all possible.

Dave DuPlantis
Agreed, what about say, Mike St. James for example? An incorrect surname would be returned.
Fry
Anne Marie van Guido. Where does the firstname stop and the last name begin?! The humanity!
Tom Ritter
Not to mention cultures where the family name is given first. Asking for the name at once is OK only so long as you always treat it as a single unit. Trying to split it will generally result in mangling some cases.
Brian
Some names also have an apostrophe or other special characters which cause problems if not handled correctly. A lot of Irish names, e.g. O'Connor, fall into this category.
Ryan
As a person who has multiple surnames, I'm getting a kick of these answers... ;-)
celopes
@Ryan: or they don't have an apostrophe, e.g. "Gearóid Ó Súilleabháin" ... for whom you may have a duplicate database record filed under "Gerard O'Sullivan" :-)
John Machin
+2  A: 

Splitting names is harder than it looks. Some names have two word last names; some people will enter a first, middle, and last name; some names have two work first names. The more reliable (or least unreliable) way to handle names is to always capture first and last name in separate fields. Of course this raises its own issues, like how to handle people with only one name, making sure it works for users that have a different ordering of name parts.

Names are hard, handle with care.

acrosman
A: 

Here's how to do it in SQL. But data normalization with this kind of thing is really a bear. I agree with Dave DuPlantis about asking for separate inputs.

JosephStyons
+43  A: 

You'll find that your key problem with this approach isn't a technical one, but a human one - different people write their names in different ways.

In fact, the terminology of "forename" and "surname" is itself flawed.

While many blended families use a hyphenated family name, such as Smith-Jones, there are some who just use both names separately, "Smith Jones" where both names are the family name.

Many european family names have multiple parts, such as "de Vere" and "van den Neiulaar". Sometimes these extras have important family history - for example, a prefix awarded by a king hundreds of years ago.

Side issue: I've capitalised these correctly for the people I'm referencing - "de" and "van den" don't get captial letters for some families, but do for others.

Conversely, many Asian cultures put the family name first, because the family is considered more important than the individual.

Last point - some people place great store in being "Junior" or "Senior" or "III" - and your code shouldn't treat those as the family name.

Also noting that there are a fair number of people who use a name that isn't the one bestowed by their parents, I've used the following scheme with some success:

Full Name (as normally written for addressing mail); Family Name; Known As (the name commonly used in conversation).

e.g:

Full Name: William Gates III; Family Name: Gates; Known As: Bill

Full Name: Soong Li; Family Name: Soong; Known As: Lisa

Bevan
+1. For applications, I usually have a "full name" and "nickname" fields, that are populated independently. I haven't had a use case for "family name", but YMMV.
erickson
+1 - PLEASE don't forget those of us blessed by our parents to use a name other than our First name - Like 'J. Edgar Hoover'. Legal Name (and CC, most times) requires 'J. Edgar Hoover'; Casual/Nickname/Known-As would be 'Edgar Hoover'. First Name, MI, Last Name don't cut it.
Ken Gentle
And some people don't have a "surname" or a "family name", e.g. in some East African cultures, they use father's name and grandfather's name e.g. Tom Dick Harry. In some cases the family nameS are in the middle e.g. Anson Maria Elizabeth Chan Fong On-sang. See also the the Wikipedia articles "Icelandic name", "Arabic name" and "Names in the Russian Empire, Soviet Union and CIS countries".
John Machin
A: 

I would specify a standard format (some forms use them), such as "Please write your name in First name, Surname form".

It makes it easier for you, as names don't usually contain a comma. It also verifies that your users actually enter both first name and surname.

Bogdan
+3  A: 

Golden rule of data - don't aggregate too early - it is much easier to glue fields together than separate them. Most people also have a middle name which should be an optional field. Some people have a plethora of middle names.

You don't need a code solution here - you need a business rule.

CAD bloke
+3  A: 

An easy way to do exactly what you asked in python is

name = "Thomas Winter"
LastName = name.split()[1]

(note the parantheses on the function call split.)

split() creates a list where each element is from your original string, delimited by whitespace. You can now grab the second element using name.split()[1] or the last element using name.split()[-1]

However, as others said, unless you're SURE you're just getting a string like "First_Name Last_Name", there are a lot more issues involved.

Baltimark
A: 

Since there are so many different variation's of how people write their names, but here's how a basic way to get the first/lastname via regex.

import re
p = re.compile(r'^(\s+)?(Mr(\.)?|Mrs(\.)?)?(?P<FIRST_NAME>.+)(\s+)(?P<LAST_NAME>.+)$', re.IGNORECASE)
m = p.match('Mr. Dingo Bat')
if(m != None):
  first_name = m.group('FIRST_NAME')
  last_name = m.group('LAST_NAME')
UberJumper
A: 

The difficulty of this problem is the exact reason why most apps require you to enter them separately.

Jeremy Cantrell
+2  A: 

If you're trying to parse apart a human name in PHP, I recomment Keith Beckman's nameparse.php script.

Jonathon Hill
And even if you're not using PHP, looking at the code is very instructive.
swillden
+1  A: 

This is a pretty old issue but I found it searching around for a solution to parsing out pieces from a globbed together name.

http://code.google.com/p/python-nameparser/

Xealot
It's very USA-centric (e.g. the titles appear to cover just about every rank in the US armed forces) and the capitalisation code produces the usual nonsenses like MacE and MacK and MacHin :-(
John Machin
A: 

It's definitely a more complicated task than it appears on the surface. I wrote up some of the challenges as well as my algorithm for solving it on my blog. Be sure to check out my Google Code project for it if you want the latest version in PHP:

http://www.onlineaspect.com/2009/08/17/splitting-names/

Josh Fraser