Syntax of partition() Method
The partition() method has a simple syntax:
string.partition(separator)
Here,
- string: The original string on which the method is applied.
- separator: The substring at which the split happens.
The method returns a tuple containing three elements:
- The part of the string before the separator.
- The separator itself.
- The part of the string after the separator.
Example
Let’s look at a complete example to understand how `partition()` works in real life:
Example 1: Separator found in the string
text = "Python is fun"
result = text.partition("is")
print(result)

You can also try this code with Online Python Compiler
Run Code
Output:
('Python ', 'is', ' fun')
Here, the string `"Python is fun"` is split into three parts:
- `'Python '` (before the separator).
- `'is'` (the separator itself).
- `' fun'` (after the separator).
Example 2: Separator not found in the string
text = "Python is fun"
result = text.partition("are")
print(result)

You can also try this code with Online Python Compiler
Run Code
Output:
('Python is fun', '', '')
In this case, since the separator `"are"` is not found in the string, the function returns the original string as the first part and two empty strings for the remaining parts.
Parameters
The partition() method accepts one parameter:
- separator (str): The string that acts as a delimiter to split the main string. If the separator is not found, the method returns a tuple with the original string and two empty strings.
Return Value
The method returns a tuple with three elements:
- First element: The substring before the separator.
- Second element: The separator itself.
- Third element: The substring after the separator.
If the separator is not found, the tuple consists of:
- The original string.
- An empty string ('').
- Another empty string ('').
Practical Use Case
The `partition()` function is particularly useful when you need to extract specific parts of a string. For example, if you have a string like `"username@domain.com"`, you can use `partition()` to separate the username, the `@` symbol, and the domain:
email = "username@domain.com"
username, separator, domain = email.partition("@")
print("Username:", username)
print("Domain:", domain)

You can also try this code with Online Python Compiler
Run Code
Output:
Username: username
Domain: domain.com
This makes it easy to process and manipulate strings in real-world applications like parsing emails, log files, or configuration data.
Using partition() with a Valid Separator
When the given separator is present in the string, partition() splits the string into three parts.
Example
text = "Python is a powerful language"
result = text.partition("is")
print(result)

You can also try this code with Online Python Compiler
Run Code
Output:
('Python ', 'is', ' a powerful language')
Explanation:
- The part before "is" is "Python ".
- The separator "is" remains as it is.
- The part after "is" is " a powerful language".
When the Separator is Not Found
If the specified separator is not present in the string, partition() returns the entire string as the first element of the tuple, followed by two empty strings.
Example
text = "Hello World"
result = text.partition("Python")
print(result)

You can also try this code with Online Python Compiler
Run Code
Output:
('Hello World', '', '')
Explanation:
- Since "Python" is not found, the entire string is placed in the first position of the tuple.
- The second and third positions are empty strings.
Working with Multiple Occurrences of the Separator
When there are multiple occurrences of the separator in the string, partition() splits only at the first occurrence.
Example
text = "apple-banana-cherry-banana"
result = text.partition("banana")
print(result)

You can also try this code with Online Python Compiler
Run Code
Output
('apple-', 'banana', '-cherry-banana')
Explanation:
- The split occurs only at the first occurrence of "banana".
- The part before "banana" is "apple-".
- The separator "banana" remains unchanged.
- The part after "banana" is "-cherry-banana".
Using Special Characters as the Separator
The partition() method works with special characters like spaces, punctuation marks, or symbols.
Example
text = "email@example.com"
result = text.partition("@")
print(result)

You can also try this code with Online Python Compiler
Run Code
Output:
('email', '@', 'example.com')
Explanation:
- The part before "@" is "email".
- The separator "@" remains unchanged.
- The part after "@" is "example.com".
Frequently Asked Questions
What happens if the separator is at the beginning of the string?
If the separator is found at the beginning, the first element of the tuple will be an empty string.
Can partition() be used with numbers?
No, the separator must always be a string. However, numbers can be converted into strings before using partition().
What is the difference between partition() and split()?
The partition() returns a tuple with three parts, splitting at the first occurrence of the separator whereas split() returns a list of all substrings split at each occurrence of the separator.
Conclusion
In this article, we explored the partition() method in Python, which is used to split a string into three parts based on a specified separator. It returns a tuple containing the part before the separator, the separator itself, and the part after it. This method is useful for structured string manipulation, making text processing more efficient in Python.