String partition() in Python

The partition() method splits the string at the first occurrence of the separator and returns a tuple containing the part the before separator, separator and the part after the separator. Here separator is a string which is given has the argument.

Syntax:
string.partition(separator)

Parameters:
The partition() method takes a separator(a string) as the argument that separates the string at the first occurrence of it.

Returns:
Returns a tuple which contains the part before the separator, separator parameter, and the part after the separator if the separator parameter is found in the string.
Returns a tuple which contains string itself and two empty strings if the separator parameter is not found.

CODE 1

filter_none

edit
close

play_arrow

link
brightness_4
code

string = "pawan is a good"
  
# 'is' separator is found
print(string.partition('is '))
  
# 'not' separator is not found
print(string.partition('bad '))
  
string = "pawan is a good, isn't it"
  
# splits at first occurence of 'is'
print(string.partition('is'))

chevron_right


Output:

('pawan ', 'is ', 'a good')
('pawan is a good', '', '')
('pawan ', 'is', " a good, isn't it")

CODE2

filter_none

edit
close

play_arrow

link
brightness_4
code

string = "geeks is a good"
  
# 'is' separator is found
print(string.partition('is '))
  
# 'not' separator is not found
print(string.partition('bad '))
  
string = "geeks is a good, isn't it"
  
# splits at first occurence of 'is'
print(string.partition('is'))

chevron_right


Output:

('geeks ', 'is ', 'a good')
('geeks is a good', '', '')
('geeks ', 'is', " a good, isn't it")


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.