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
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')) |
Output:
('pawan ', 'is ', 'a good')
('pawan is a good', '', '')
('pawan ', 'is', " a good, isn't it")
CODE2
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')) |
Output:
('geeks ', 'is ', 'a good')
('geeks is a good', '', '')
('geeks ', 'is', " a good, isn't it")
Recommended Posts:
- numpy.partition() in Python
- Python | Numpy matrix.partition()
- Python | Pandas Series.str.partition()
- String slicing in Python to check if a string can become empty by recursive deletion
- Python | Check if given string can be formed by concatenating string elements of list
- Python | Check if string ends with any string in given list
- Python | Check if a given string is binary string or not
- String slicing in Python to rotate a string
- Python String | min()
- Python String | max()
- Python String
- Python | Add one string to another
- string.whitespace in Python
- Python | String upper()
- String Formatting in Python using %
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.



