Python - String Method - partition() Tutorial
This method will split the string into three-part based on the first occurrence of the argument string and return it in the form of a tuple.
The first part will be the string before the first occurrence of the argument, the second part is itself an argument and the third part will be the string after the first occurrence of the argument.
Syntax-
String.partition(separator)
#separator - It is a string which will split the original string
into 3 parts.
Example-
str1 = "Sam is good in math, but he is bad in english"
# 'is' separator is found, wil split it at first occurence
print(str1.partition('is '))
# 'not' separator is not found
print(str1.partition('not '))
Output-
('Sam ', 'is ', 'good in math, but he is bad in english')
('Sam is good in math, but he is bad in english', '', '')
In the above program, if the argument is not found in the string, then in first part it will return whole string, second and third part will be returned empty.