The Wayback Machine - https://web.archive.org/web/20240717024524/https://www.geeksforgeeks.org/python-string-partition-method/
Open In App

Python String partition() Method

Last Updated : 22 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Example: 

Python3




str = "I love Geeks for geeks"
print(str.partition("for"))


Output: 

('I love Geeks ', 'for', ' geeks')

Python String partition() Method Syntax

Syntax: string.partition(separator)

Parameters: 

  • separator: a substring that separates the string

Return: Returns a tuple of 3 elements. The substring before the separator, the separator, the part after the separator.

String partition() in Python Examples

Working of Python String partition() Method

In this example, we are demonstrating the use of the partition() method in Python with two separators attracts and is.

Python3




string = "light attracts bug"
# 'attracts' separator is found
print(string.partition('attracts'))
 
string = "gold is heavy"
# 'is' as partition
print(string.partition('is'))


Output: 

('light ', 'attracts', ' bug')
('gold ', 'is', ' heavy')

The partition() Method only partitions on the first occurrence of the separator

In this example, we are showing that the Python String partition() Method only considers the first occurrence of the separator in the string. When the first occurrence of the separator will occur the string will be separated.

Python3




string = "b follows a, c follows b"
print(string.partition('follows'))
 
string = "I am happy, I am proud"
print(string.partition('am'))


Output: 

('b ', 'follows', ' a, c follows b')
('I ', 'am', ' happy, I am proud')

Result of partition() Method when the separator is missing from the String

In this example, We are showing that the Python String partition() Method returns the original string as the first element of the tuple, and the remaining two elements are empty. As you can see ‘are’ is missing from the string ‘geeks for geeks’.

Python3




string = "geeks for geeks"
print(string.partition('are'))


Output:

('geeks for geeks', '', '')

Used Partition() Method to get a domain name from the String

In this example, We are using the partition() to get the domain name of the URL String. 

Python3




result = url.partition("//")
result = result[2].partition("/")
print(result[0])


Output:

www.example.com

Case sensitive Python

In this example, we are using two different case-sensitive separators fox and Fox, which return different tuples.

Python3




sentence = "The quick Brown fox jumps over the lazy Fox."
result = sentence.partition("fox")
print(result)
result = sentence.partition("Fox")
print(result)


Output:

('The quick Brown ', 'fox', ' jumps over the lazy Fox.')
('The quick Brown fox jumps over the lazy ', 'Fox', '.')


Previous Article
Next Article

Similar Reads

Python | sympy.partition() method
With the help of sympy.partition() method, we can find Partition number in SymPy. partition(n) - The Partition numbers are a sequence of integers pn that represent the number of distinct ways of representing n as a sum of natural numbers (with order irrelevant). The generating function for pn is given by - [Tex]\sum_{n=0}^\infty p_n x^n = \prod_{k=
1 min read
Python | sympy Partition() method
With the help of sympy.combinatorics.partitions.Partition() method, we are going to separate the members of arrays into one tuple by using sympy.combinatorics.partitions.Partition() method. Syntax : sympy.combinatorics.partitions.Partition(array) Return : Return a members of arrays into one tuple. Example #1 : In this example we can see that by usi
1 min read
Python | SymPy Partition.sort_key() method
Partition.sort_key() : sort_key() is a sympy Python library function that returns canonical key which can be used for string. This ordering of canonical key is based on the size and the elements of the partition whose ties are broken with the rank. Syntax : sympy.combinatorics.partitions.Partition.sort_key() Return : Canonical key which can be used
1 min read
Python | Pandas Series.str.partition()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas str.partition() works in a similar way like str.split(). Instead of splitting the string at every occurrence of separator/delimit
3 min read
numpy.partition() in Python
numpy.partition() function is used to create a partitioned copy of input array with its elements rearranged in such a way that the value of the element in k-th position is in the position it would be in a sorted array. All elements smaller than the k-th element are moved before this element and all equal or greater are moved behind it. The ordering
2 min read
Python | Numpy matrix.partition()
With the help of Numpy matrix.partition() method, we are able to partition the matrix in such a way that index value that we pass sort the matrix like all the values smaller than that value is move left to it and others are right with the help of matrix.partition() method. Syntax : matrix.partition(index) Return : Return partitioned matrix Example
1 min read
SymPy | Partition.RGS_generalized() in Python
Partition.RGS_generalized() : RGS_generalized() is a sympy Python library function that calculates the m + 1 generalized unrestricted growth strings (string where a[i] is the block in which element i occurs) and then return it as rows in matrix. Restricted growth strings - string in which each character, ai results in the Block (Bi) in which the co
1 min read
SymPy | Partition.RGS_enum() in Python
Partition.RGS_enum() : RGS_enum() is a sympy Python library function that calculates the total number of restricted growth strings (string where a[i] is the block in which element i occurs) which is possible for a superset of size n. Restricted growth strings - string in which each character, ai results in the Block (Bi) in which the corresponding
2 min read
SymPy | Partition.RGS_rank() in Python
Partition.RGS_rank(rank, n) : RGS_rank() is a sympy Python library function that gives the rank for a restricted growth strings (string where a[i] is the block in which element i occurs) . Restricted growth strings - string in which each character, ai results in the Block (Bi) in which the corresponding element belongs. Syntax : sympy.combinatorics
1 min read
SymPy | Partition.RGS_unrank() in Python
Partition.RGS_unrank(rank, n) : RGS_unrank() is a sympy Python library function that gives the unranked restricted growth strings (string where a[i] is the block in which element i occurs) for a superset of size n. Restricted growth strings - string in which each character, ai results in the Block (Bi) in which the corresponding element belongs. Sy
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg