Python — Split String in Half

Here is an easy way to split a string in two. I just have a rudimentary form collecting a full name, yet my model stores first/last names.

I’m just going to use the partition string method.

‘Yuji Tomita’.partition(‘ ‘)
returns (‘Yuji’, ‘ ‘, ‘Tomita)

‘Yuji Middlename Tomita’.partition(‘ ‘)
returns (‘Yuji’, ‘ ‘, ‘Middlename Tomita’)

Which is plenty good for me.

Leave a Comment