Table of contents
1.
Introduction
2.
How does partition() work?
3.
Syntax of partition() Method
4.
Example  
4.1.
Example 1: Separator found in the string
4.2.
Example 2: Separator not found in the string
5.
Parameters
6.
Return Value
7.
Practical Use Case  
8.
Using partition() with a Valid Separator
8.1.
Example
9.
When the Separator is Not Found
9.1.
Example
10.
Working with Multiple Occurrences of the Separator
10.1.
Example
11.
Using Special Characters as the Separator
11.1.
Example
12.
Frequently Asked Questions
12.1.
What happens if the separator is at the beginning of the string?
12.2.
Can partition() be used with numbers?
12.3.
What is the difference between partition() and split()?
13.
Conclusion
Last Updated: Mar 15, 2025
Easy

Python String partition() Method

Author Rahul Singh
0 upvote

Introduction

The partition() method in Python 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 the separator. This method is useful for breaking strings into structured components. 

Python String partition() Method

In this article, we will learn how the partition() method works, its syntax, and examples to demonstrate its implementation.

How does partition() work?

The `partition()` function in Python is a string method that splits a string into three parts based on a specified separator. It searches for the first occurrence of the separator in the string and divides the string into three parts:  

1. The part before the separator.  
 

2. The separator itself.  
 

3. The part after the separator.  


If the separator is not found in the string, the function returns the original string as the first part and two empty strings for the remaining parts.  

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:

  1. The part of the string before the separator.
     
  2. The separator itself.
     
  3. 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:

  1. The original string.
     
  2. An empty string ('').
     
  3. 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.

Live masterclass