The partition Method in Python: Splitting a String into Three Parts

The partition method splits a string into three parts (prefix, separator, suffix) and returns them as a tuple. Splitting occurs from left to right.

partition(sep)

  • sep -- separator string used to split the original string. It can have one or more characters.

Returns a tuple with three elements:

my_str = ''
my_str.partition('.')  # ('', '', '')

my_str = '12'
my_str.partition('.')  # ('12', '', '')

my_str = '.1.2'
my_str.partition('.')  # ('', '.', '1.2')