Splitting a person's name into forename and surname

前端 未结 16 1943
北海茫月
北海茫月 2020-12-05 10:46

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

16条回答
  •  有刺的猬
    2020-12-05 11:08

    This is how I do it in my application:

    def get_first_name(fullname):
        firstname = ''
        try:
            firstname = fullname.split()[0] 
        except Exception as e:
            print str(e)
        return firstname
    
    def get_last_name(fullname):
        lastname = ''
        try:
            index=0
            for part in fullname.split():
                if index > 0:
                    if index > 1:
                        lastname += ' ' 
                    lastname +=  part
                index += 1
        except Exception as e:
                print str(e)
        return lastname
    
    def get_last_word(string):
        return string.split()[-1]
    
    print get_first_name('Jim Van Loon')
    print get_last_name('Jim Van Loon')
    print get_last_word('Jim Van Loon')
    

提交回复
热议问题