string.split escaping

Andrew Bennetts andrew-pythonlist at puzzling.org
Thu May 29 02:18:43 EDT 2003


On Thu, May 29, 2003 at 04:03:37PM +1000, Dave Harrison wrote:
> If I have a string 
> 
> a = 'dog;cat;big;dog'
> 
> How do I escape the last ';' so that when I call 
> 
> string.split(a, ';')
> 
> I get
> 
> ['dog', 'cat', 'big;dog']

If you know how many splits you want, use the optional "maxsplit" argument:

>>> 'dog;cat;big;dog'.split(';', 2)
['dog', 'cat', 'big;dog']

-Andrew.






More information about the Python-list mailing list