split() off last substring

Peter Hansen peter at engcorp.com
Sun Apr 14 23:38:51 EDT 2002


bvdpoel at uniserve.com wrote:
> 
> Can I tell split() to split off the LAST seqment? I guess I want
> something like:
> 
>         a="aaa.bbb.ccc"
>         b,c=a.rsplit(1)
>         print b,c
>         # "aaa.bbb", "ccc"

How about this:

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
>>> a = 'aaa.bbb.ccc'
>>> a.split('.')
['aaa', 'bbb', 'ccc']
>>> b = a.split('.')[:-1]
>>> b = '.'.join(b)
>>> c = a.split('.')[-1]
>>> b,c
('aaa.bbb', 'ccc')

-Peter



More information about the Python-list mailing list