Python: Why Does str.split() Return a list While str.partition() Returns a tuple?

The key difference between those methods is that split() returns a variable number of results, and partition() returns a fixed number. Tuples are usually not used for APIs which return a variable number of items.


@yole answer summarise the reasoning why partition() returns tuple. But there is a nice way to "exploit" that fact. I found below example in "Automate the boring stuff with Python".

   before, sep, after = 'Hello, world!'.partition(' ')
   print(before)

Tags:

Python