In this Python snippet post we're going to look at a lesser known string method called partition. It's a lot like the split method, but with a few crucial differences.

A quick split recap

For those of you not familiar with split, it's a string method which is used to break a string up into multiple parts based on some separator. For example, we might have something like this:

price = 12.50
split_price = price.split(".")  # ['12', '50']

Often you'll destructure this list into several variables like this:

prince = 12.50
dollars, cents = prince.split(".")

print(dollars)  # 12
print(cents)    # 50

There are a few things to note here. For one,  split only cares about the values on either side of a separator. The separator itself just gets thrown out.

The second thing to be aware of, is that we don't know how many values we're going to get out of split, at least not unless we specify in some additional configuration. split will will keep dividing the string into new list items every time it finds the separator in the string.

Differences with partition

So what about partition? Well let's take a look:

price = 12.50
partition_price = price.partition(".")  # ('12', '.', '50')

Right away we can see one difference: partition kept the separator character. This is interesting, and I think can be quite useful when we want to reuse this separator to reform the string after some modifications. We can be quite dynamic with this, while making use of good names.

So what about when we encounter multiple instances of the separator character?

file_name = "grand_canyon.005.png"
partition_file_name = file_name.partition(".")  # ("grand_canyon", ".", "005.png")

Despite there being two instances of . in the string, we still ended up with a three item tuple. And yes, it is a tuple, not a list.

partition will always give us a three item tuple. The first item will contain the string right up until the first instance of the separator; the second item is the separator itself; and the third item is everything which came after this first instance of the separator.

So what about if the separator character isn't in the string?

name = "Jose"
partition_name = name.partition(".")  # ('Jose', '', '')

Once again, we get a three item tuple, but when the separator wasn't found, the second and third items are empty strings. The first item will contain the entire string the method was called on.

The rpartition variant

In addition to partition, there's also an rpartition method, which starts scanning from the end of the string. This means that the first encountered instance of the separator is the last one to occur in the string. This might be useful for grabbing a file extension, the separator, and the filename all in one go, for example.

file_name = "grand_canyon.005.png"

name, seperator, extension = file_name.rpartition(".")

Wrapping Up

That's it for this snippet post! Hopefully you learnt something new, and I hope you find a use for partition in your own projects!

If you're just starting out with Python, or want to upgrade your skills, we'd love to have you on our Complete Python Course. You might also want to subscribe to our mailing list below. It's a great way to stay up to date with all our content, and we also post regular coupons for our courses!