Related Articles

Related Articles

String partition() in Python
  • Last Updated : 19 Nov, 2019

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 occurrence 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 occurrence of 'is'
print(string.partition('is'))

chevron_right


Output:

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :