[Tutor] help with strings

Kent Johnson kent37 at tds.net
Wed Feb 3 16:25:28 CET 2010


On Wed, Feb 3, 2010 at 8:19 AM, NISA BALAKRISHNAN
<snisa.balakrishnan at gmail.com> wrote:
> hi
>
> I am very new to python.
> I have a string for example : 123B     new Project
> i want to separate 123B as a single string and new  project as another
> string .
> how can i do that.
> i tried using partition but couldnt do it

str.split() is the thing to use. By default it splits on any white space:

In [1]: s = "123B     new Project"

In [2]: s.split()
Out[2]: ['123B', 'new', 'Project']

You can tell it to only split once (the None argument means "split on
white space"):
In [5]: s.split(None, 1)
Out[5]: ['123B', 'new Project']

The result is a list; you can assign each element to a variable:
In [6]: first, second = s.split(None, 1)

In [7]: first
Out[7]: '123B'

In [8]: second
Out[8]: 'new Project'

Kent


More information about the Tutor mailing list