Python String partition() Method Examples

In the previous article, we have discussed Python Program for lstrip() Function
partition() Function in Python:

The partition() method looks for a specified string and splits it into a tuple with three elements.

The part preceding the specified string is contained in the first element.

The specified string is contained in the second element.

The part following the string is contained in the third element.

Note: partition() method will look for the first occurrence of the specified string.

Syntax:

string.partition(value)

Parameters

value: This is required. The string to look for.

If the specified value is not found, the partition() method returns a tuple containing the following values: 1 – the entire string, 2 – an empty string, 3 – an empty string.

For example:

gvn_str = "welcome to python learning platform"
rslt_tupl = gvn_str.partition("java")
print(rslt_tupl)

Output:

('welcome to python learning platform', '', '')

Examples:

Example1:

Input:

Given string = "welcome to python learning platform"
Given value = "python"

Output:

The result tuple after applying partition() function on the given string : 
('welcome to ', 'python', ' learning platform')

Example2:

Input:

Given string = "good morning all"
Given value = "all"

Output:

The result tuple after applying partition() function on the given string : 
('good morning ', 'all', '')

Program for partition() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Give the value as static input and store it in another variable.
  • Apply partition() function to the given string for the given value to split the given string into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the partition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to python learning platform"
# Give the value as static input and store it in another variable.
gvn_valu = "python"
# Apply partition() function to the given string for the given value to split
# the given string into a tuple with three elements as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.partition(gvn_valu)
# Print the result tuple after applying partition() function on the given string
# for the given value
print("The result tuple after applying partition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = "good morning all"
gvn_valu2 = "all"
rslt_tupl2 = gvn_str2.partition(gvn_valu2)
print("The result tuple after applying partition() function on the given second string : ")
print(rslt_tupl2)

Output:

The result tuple after applying partition() function on the given string : 
('welcome to ', 'python', ' learning platform')
The result tuple after applying partition() function on the given second string : 
('good morning ', 'all', '')

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Give the value as user input using the input() function and store it in another variable.
  • Apply partition() function to the given string for the given value to split the given string into a tuple with three elements as mentioned above.
  • Store it in another variable.
  • Print the result tuple after applying the partition() function on the given string for the given value.
  • Similarly, do the same for the other string and print the result string.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Give the value as user input using the input() function and
# store it in another variable.
gvn_valu = input("Enter some random value = ")
# Apply partition() function to the given string for the given value to split
# the given string into a tuple with three elements as mentioned above.
# Store it in another variable.
rslt_tupl = gvn_str.partition(gvn_valu)
# Print the result tuple after applying partition() function on the given string
# for the given value
print("The result tuple after applying partition() function on the given string : ")
print(rslt_tupl)
# Similarly, do the same for the other string and print the result string
gvn_str2 = input("Enter some random string = ")
gvn_valu2 = input("Enter some random value = ")
rslt_tupl2 = gvn_str2.partition(gvn_valu2)
print("The result tuple after applying partition() function on the given second string : ")
print(rslt_tupl2)

Output:

Enter some random string = this is python coding platform
Enter some random value = coding
The result tuple after applying partition() function on the given string : 
('this is python ', 'coding', ' platform')
Enter some random string = hello all 12345
Enter some random value = 12345
The result tuple after applying partition() function on the given second string : 
('hello all ', '12345', '')

Are you facing difficulties in finding all the methods that a string object can call in python? Have a glance at this Python String Method Examples Tutorial & meet such challenges with ease.