66 of 151 menu

The partition method

The partition method returns a tuple of a string split at the first match of a substring and the specified delimiter parameter.

Syntax

string.partition(separator)

Example

Let's apply the partition method to the following line:

txt = 'abc_dea' print(txt.partition('_'))

Result of code execution:

('abc', '_', 'dea')

Example

Now let's add an extra character '_' to our string and apply the partition method again:

txt = 'ab_cd_ea' print(txt.partition('_'))

As can be seen from the obtained result, the method split the string only by the first match of the character '_':

('ab', '_', 'cd_ea')

See also

  • method rpartition,
    which divides a string by the last match of a substring
  • method rsplit,
    which divides a string by the substring on the right
  • method split,
    which divides a string by the substring on the left
  • method join,
    which returns a string from a list of strings
byenru