partition() in Python | Python String partition()

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The partition() function in Python splits the string at the first occurrence of the specified separator argument and returns a tuple containing the part before the separator, the separator itself, and the part after the separator.

Syntax of partition() in Python

Parameters of partition() in Python

The partition()function in Python takes a separator(a string) as the argument that separates the string at its first occurrence.

Return values of partition() in Python

  1. If the specified separator is found in the string, a tuple (A Tuple is an immutable and ordered collection of python objects separated by commas) is returned that contains the part before the separator, separator parameter, and the part after the separator.
  2. If the specified separator is not found, then the partition() function returns the string itself as a first element and two empty string elements.

Example of partition() in Python

The following example illustrates the working of partition method in python.

Code:

Output:

In the above program, we created a string named test_str and stored "Scaler/Academy". In the following line, we have implemented the partition function on our test_str and passed , as the separator; therefore, we got the tuple ('Scaler', '/', 'Academy') as the output.

What is partition() in Python?

What is partition in python

Python String partition() method splits the string at the first occurrence of the separator. It returns a tuple containing the part before the separator, the separator, and the part after the separator. Here separator is a string that is given as the argument.

If the matching separator is not found in the string, then a tuple that contains three elements, which are the string itself as a first element and two empty string elements, is returned.

More Examples

Example 1: The partition method is Case-Sensitive

Let's understand how the partition method is case-sensitive with the help of an example.

Code:

Output:

Explanation: In the above program, Academy and academy are treated as two different separators, so two different tuples are returned.

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.

Example 2: If the separator is an empty string

If the separator is an empty string, the partition() method will throw ValueError*. Let's understand how the partition method is case-sensitive with the help of an example.

Code:

Output:

Explanation:

In the above program, we have created a test_str and stored 'Scaler Academy' in it, on which the partition function is called. Since the separator is an empty string , we will get a ValueError.

Example 3: If the separator is not found

If the separator is not present in the string, then the tuple contains the original string and two empty strings, as shown in the example below.

Code:

Output:

Explanation:

In the above program, when the partition function is called on test_str with a separator that does not exist in the string, we did get a tuple containing the original string and two empty strings.

Example 4: Separators passed can be numbers as well as symbols

Code:

Output:

Explanation: In the above program, the partition() method is called on test_str using '1' as well as '#' as the separator.

Conclusion

  • The partition() string method in python searches for a specified string and splits the string into a tuple containing three elements.
  • If the separator passed as an argument to the partition() function is not present in the string, then the tuple contains the original string and two empty strings.
  • The partition string method is case-sensitive, and separators passed as an argument can be characters, strings, numbers, and symbols.

See Also