Python String partition()

The syntax of partition() is:

string.partition(separator)

partition() Parameters()

The partition() method takes a string parameter separator that separates the string at the first occurrence of it.


Return Value from partition()

The partition method returns a 3-tuple containing:

  • the part before the separator, separator parameter, and the part after the separator if the separator parameter is found in the string
  • the string itself and two empty strings if the separator parameter is not found

Example: How partition() works?

string = "Python is fun"

# 'is' separator is found
print(string.partition('is '))

# 'not' separator is not found
print(string.partition('not '))

string = "Python is fun, isn't it"

# splits at first occurence of 'is'
print(string.partition('is'))

Output

('Python ', 'is ', 'fun')
('Python is fun', '', '')
('Python ', 'is', " fun, isn't it")

Also Read:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community