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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s