Python String partition() Method
In Python, the String partition() method splits the string into three parts at the first occurrence of the separator and returns a tuple containing the part before the separator, the separator itself, and the part after the separator.
Let’s understand with the help of an example:
s = "Geeks geeks"
res = s.partition("for")
print(res)
Output
('Geeks geeks', '', '')
Explanation:
- The string is split into three parts:
- Part before the separator:
'I love Geeks' - The separator:
'for' - Part after the separator:
' Geeks'
- Part before the separator:
- If the separator is found, the method returns a tuple containing these three components.
Table of Content
Syntax of partition() method
string.partition(separator)
Parameters:
- separator(required): a substring that separates the string
Return Type:
- Returns a tuple of three strings:
(before_separator, separator, after_separator). - If the separator is not found, the tuple returned is
(string, '', '').
Using partition() with a valid separator
This example demonstrates the behavior when the separator exists in the string:
s = "Python is fun"
res = s.partition("is")
print(res)
Output
('Python ', 'is', ' fun')
Explanation:
- The string is split at the first occurrence of the separator
"is". - The method returns a tuple containing the three parts of the string.
When the separator is not found
If the separator is absent, the partition() method returns a specific result.
s = "Python is fun"
res = s.partition("Java")
print(res)
Output
('Python is fun', '', '')
Explanation:
- Since the separator
"Java"is not present in the string, the entire string is returned as the first element of the tuple. - The second and third elements are empty strings.
Working with multiple occurrences of the separator
partition() method only considers the first occurrence of the separator:
s = "Learn Python with GeeksforGeeks"
res = s.partition("Python")
print(res)
Output
('Learn ', 'Python', ' with GeeksforGeeks')
Explanation:
- The string is split at the first occurrence of
"Python". - Subsequent occurrences of the separator are ignored by the
partition()method.
Using special characters as the separator
partition() method works seamlessly with special characters:
s = "key:value"
res = s.partition(":")
print(res)
Output
('key', ':', 'value')
Explanation:
- The string is split into three parts at the occurrence of
":". - This method is particularly useful when parsing key-value pairs in a structured format.
Frequently Asked Questions on String partition() method
1. What happens if the separator is at the beginning or end of the string?
If the separator is at the beginning or end, the corresponding part in the tuple will be an empty string:
s = ":start"print(s.partition(":")) # Output: ('', ':', 'start')text = "end:"print(s.partition(":")) # Output: ('end', ':', '')
2. Can the partition() method handle multi-character separators?
Yes, the
partition()method can work with multi-character separators:s = "Hello>>>World"print(s.partition(">>>")) # Output: ('Hello', '>>>', 'World')
3. Is the partition() method case-sensitive?
Yes, the method is case-sensitive:
s = "CaseSensitive" print(s.partition("casesensitive")) # Output: ('CaseSensitive', '', '')


