{"@context":"https://schema.org","@type":"Article","mainEntityOfPage":{"@type":"WebPage","@id":"https://www.tutorialsteacher.com/python/string-partition"},"headline":"Python String partition() Method","author":{"@type":"Organization","name":"TutorialsTeacher","url":"https://www.tutorialsteacher.com/aboutus"},"publisher":{"@type":"Organization","name":"TutorialsTeacher","logo":{"@type":"ImageObject","url":"https://www.tutorialsteacher.com/Content/images/logo.svg"},"sameAs":["https://www.facebook.com/tutorialsteacher","https://twitter.com/tutorialstchr","https://www.youtube.com/@tutorialsteacherOfficial"]},"dateModified":"2021-06-26T15:03:44.2351855Z","description":"The partition() method splits the string at the first occurrence of the specified string separator sep argument and returns a tuple containing three elements, the part before the separator, the separator itself, and the part after the separator."}sep argument and returns a tuple containing three elements, the part before the separator, the separator itself, and the part after the separator." />

Python String partition() Method

The partition() method splits the string at the first occurrence of the specified string separator sep argument and returns a tuple containing three elements, the part before the separator, the separator itself, and the part after the separator.

If the matching separator is not found in the string then returns a tuple containing three elements, the string itself as a first element and two empty string elements.

Syntax:

str.partition(sep)

Parameters:

sep: (Required) A string separator.

Return Value:

  1. Returns tuple that contains three elements
  2. The part before the separator as a first element, the separator itself as a second element, and the part after the separator as a third element if the specified separator is found.
  3. If the specified separator is not found, then returns the string itself as a first element and two empty string elements.

The following example demonstrates the partition() method:

Example: partition()
mystr = 'Hello World'
print(mystr.partition(' '))

mystr = 'How are you?'
print(mystr.partition('are'))
Output
('hello', ' ', 'world')
('How ', 'are', ' you?')

The partition() method search is case-sensitive. It treats 'Tutorials' and 'tutorials' as two separate words, as shown below.

Example: partition()
mystr = 'TutorialsTeacher is the best tutorials website.'
print(mystr.partition('tutorials'))
print(mystr.partition('Tutorials'))
Output
('TutorialsTeacher is the best ', 'tutorials', ' website')
('', 'Tutorials', 'Teacher is the best tutorials website')

In the above example, 'tutorials' and 'Tutorials' are treated as two different separators and so returns two different tuples. Note that if a separator string is found at the start of a string, then the first element of a returning tuple will be empty, as in the case of 'Tutorials' separator in the above example.

If the separator is not present in the string, then the tuple contains the original string and two empty strings, as shown below. If the separator is an empty string, then the partition() method will throw ValueError, as shown below.

Example: partition()
mystr = 'Hello World'
print(mystr.partition('s'))

mystr = 'Hello World'
print(mystr.partition(''))
Output
('Hello World', '', '')
ValueError: empty separator

The separator passed can be numbers as well as symbols.

Example: partition()
mystr = '#1 Harbor Side'
print(mystr.partition('1'))
Output
('#', '1', ' Harbor Side')

The partition() method will only split the string at the first occurrence of the separator.

Example: partition()
mystr="TutorialsTeacher"
print(mystr.partition("T")
Output
 ('', 'T', 'utorialsTeacher') 

The partition() method performs case-sensitive search.

Example: partition()
mystr="TutorialsTeacher"
print(mystr.partition("t")
Output
 ('Tu', 't', 'orialsTeacher')
Want to check how much you know Python?