< Prev
Next >

Python - String partition() Method

As per the name suggests, the partition() string method is used to partition the content of the invoked string by searching for a specific value. If the searched value is found, the partition() method returns a tuple of three split values, where -

Signature of partition() method


Parameters Description
This is the value to be searched in the invoked string and on the basis of it a tuple of three values will be created, only if this value is found.

Even when the value to be searched is having multiple occurrences within the invoked string, but the partition() method partitions the string only at the first occurrence of the searched value and the rest of occurrences of the searched value are ignored.

Note : The partition() method begins its search at the left of the invoked string. Let us see an example.

# Python - Example of partition() method.

# Creating a string 
s1 = 'Eddie edited a diary!'

# Printing the value of string 
print('String is : ', s1)

# Calling the partition() method on string
# which will perform the number of splits on the basis of searched value  i.e. di
# and returns a tuple containing all the split values
tuple1 = s1.partition('di')

# Priting all the split values 
print('A tuple split by di : ', tuple1)

Output is :

String is :  Eddie edited a diary!
A tuple split by di :  ('Ed', 'di', 'e edited a diary!')

As you can see in the example, even though the string had 3 occurrences of the searched value ed but still the partition() method partitioned the string at the first occurrence of the searched value ed.

Please share this article -

< Prev
Next >
< String splitlines() method
String rpartition() Method >


Please Subscribe

Please subscribe to our social media channels for daily updates.

Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page



Please check our latest addition