Methods and Functions

Python String partition()

Python’s string methods have different functionalities. By using these built-in string module functions we can perform specific operations on the string iterables. One such operation is string partition. Python partition() function lets us separate the string passed as an argument to the function by specifying the separator value. This article focuses on Python partition(), its applications, and a few basic examples to understand more about the Python partition().

Python partition() function

Definition

  • Python partition() function is used to partition a string at the first occurrence of the given string and return a tuple that includes 3 parts – the part before the separator, the argument string (separator itself), and the part after the separator.
  • The Python partition() string method searches for the specified separator substring and returns a tuple with 3 strings namely – everything before the separator, the separator itself, and everything after it.

partition() Syntax

The Python partition string function divides a given string using the provided separator and returns a tuple with three arguments. The syntax for Python string partition() is as follows:

                    

string.partition(separator)

partition() Parameters

The partition() function accepts a single parameter:

separator – a string parameter that separates the string at the first occurrence of it.

Note – If the separator argument is kept empty, then the Python interpreter will throw a TypeError exception.

Return value from partition()

The partition() string method returns a 3-tuple containing the following:

  • If the separator parameter is found in the string, the portion before the separator, the separator parameter, and the part after the separator is returned.
  • If the separator parameter is not found, then a tuple with 2 empty strings, followed by the string itself is returned.

For example, if we have A*B*C as our string input and we use * as a separator, the partition() function looks for * from left to right. When it discovers the * symbol, it returns the string succeeding the * symbol as Tuple Item 1 (A), and the * symbol as Tuple Item 2. And the remainder of the string as Tuple Item 3 (B*C).

Example 1: How partition() works?

Let us look at a basic Python program to understand how the Python partition() actually works.

Example

                    

# Python program to illustrate partition()
string = 'I love to watch TV shows'

# when separator is found
print(string.partition('to'))

# when separator is not found
print(string.partition('who'))

string2 = 'Cricket is fun to watch, isnt it true?'
# when separator occurs multiple times in the string
print(string2.partition('is'))

Output

                    

('I love ', 'to', ' watch TV shows')
('I love to watch TV shows', '', '')
('Cricket ', 'is', ' fun to watch, isnt it true?')

If a string has two occurrences of the separator string, it is divided at the first occurrence, i.e. it searches for the separator from the Left-Hand side. This can be seen in the above example, where string2.rpartition(‘is’) splits from the first occurrence of ‘is’.

Example 2: Empty separator in partition()

What happens if we do not specify any separator to the partition() function? Look at the below program to understand.

Example

                    

mystr = 'Good Morning, Tommy'
print(mystr.partition())

Output

                    

Traceback (most recent call last):
File "", line 2, in 
TypeError: partition() takes exactly one argument (0 given)

Difference between partition() and rpartition()

The string module has various operative functions. These being Python’s partition() and rpartition(). Now what’s the main difference between these two? They both function in a similar manner. The only difference is that the partition() method splits the string from the first occurrence of the separator, while the rpartition() separates the string from the last occurrence of the separator.

Example

                    

mystr = 'Hello World'

print(mystr.partition('o'))
print(mystr.rpartition('o'))

Output

                    

('Hell', 'o', ' World')
('Hello W', 'o', 'rld')

Frequently Asked Questions

Q1. What is partition() method in Python?

Python partition() function is used to partition a string at the first occurrence of the given string and return a tuple that includes 3 parts – the part before the separator, the argument string (separator itself), and the part after the separator.

Q2. What does split() do in Python?

Python split() is a string method that is used to return a list of strings by splitting the input string from the left at the specified separator i.e. splits from the first occurrence of the specified separator.

Q3. How do you split a word in Python?

We can split words in Python by using 2 built-in functions known as the Python split() function and the Python rsplit() function.

The difference between split() and rsplit() is the use of the maxsplit argument. If the maxsplit argument is set, the rsplit() function splits a string from the right side (from the final character), whereas the split() method splits from the left side (from the first character).

The syntax for both the functions are as below:

                    

string.split(separator, maxsplit)
string.rsplit(separator, maxsplit)

Example

                    

string = 'Hey how are you?'
print(string.split(' '))

my_str = 'I love Python programming !!!'
print(my_str.rsplit())

Output

                    

['Hey', 'how', 'are', 'you?']
['I', 'love', 'Python', 'programming', '!!!']

Q4. What does rstrip() do in Python?

The rstrip() function in Python eliminates all trailing characters from a string. It indicates that it removes all of the specified characters from the string’s right side. If the parameter is not specified, it removes all whitespaces from the string. The rstrip() method returns a string copy with the trailing characters deleted (based on the string argument passed).

Example

                    

string = 'Python Programming'
print(string.rstrip('ing'))

Output

                    

Python Programm

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.