Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i split a full name to first name and last name in python?

I'm a novice in python programming and i'm trying to split full name to first name and last name, can someone assist me on this ? so my example file is:

Sarah Simpson

I expect the output like this : Sarah,Simpson

like image 234
ruddy simonpour Avatar asked Oct 18 '25 00:10

ruddy simonpour


1 Answers

You can use the split() function like so:

fullname=" Sarah Simpson"
fullname.split()

which will give you: ['Sarah', 'Simpson']

Building on that, you can do:

first=fullname.split()[0]
last=fullname.split()[-1]
print(first + ',' + last)

which would give you Sarah,Simpson with no spaces

like image 186
user138278 Avatar answered Oct 20 '25 17:10

user138278