Splitting a person's name into forename and surname

前端 未结 16 1999
北海茫月
北海茫月 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:12

    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.

提交回复
热议问题