Saturday, May 23, 2015

Python split with examples

"""
Rsplit. Usually rsplit() is the same as split.
The only difference occurs when the second argument is specified. 
This limits the number of times a string is separated.

So:
When we specify 3, 
we split off only three times from the right. 
This is the maximum number of splits that occur.
Source:#http://www.dotnetperls.com/split-python
"""
In [1]: s="Obama,President,USA,Ajay,cam,cyber"

In [2]: s.rsplit(",",3)
Out[2]: ['Obama,President,USA', 'Ajay', 'cam', 'cyber']

In [4]: s.split(",",3)
Out[4]: ['Obama', 'President', 'USA', 'Ajay,cam,cyber']

In [5]: s = "foo    bar   foobar foo"

In [7]: s.split(None,2)
Out[7]: ['foo', 'bar', 'foobar foo']

Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.