new string method in 2.5 (partition)

John Salerno johnjsal at NOSPAMgmail.com
Tue Sep 19 15:17:21 EDT 2006


richard.charts at gmail.com wrote:
> I'm confused.
> What's the difference between this and string.split?

 >>> s = 'hello, world'

 >>> s.split(',')
['hello', ' world']

 >>> s.partition(',')
('hello', ',', ' world')


split returns a list of the substrings on either side of the specified 
argument.

partition returns a tuple of the substring on the left of the argument, 
the argument itself, and the substring on the right. rpartition reads 
from right to left.


But you raise a good point. Notice this:

 >>> s = 'hello, world, how are you'

 >>> s.split(',')
['hello', ' world', ' how are you']

 >>> s.partition(',')
('hello', ',', ' world, how are you')

split will return all substrings. partition (and rpartition) only return 
the substrings before and after the first occurrence of the argument.



More information about the Python-list mailing list