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 | sympy Partition() method
- SymPy | Partition.prev_lex() in Python
- SymPy | Partition.RGS_generalized() in Python
- Python | Numpy matrix.partition()
- SymPy | Partition.RGS_enum() in Python
- SymPy | Partition.RGS_unrank() in Python
- SymPy | Partition.as_dict() in Python
- SymPy | Partition.next_lex() in Python
- SymPy | Partition.conjugate() in Python
- SymPy | Partition.as_ferrers() in Python
- SymPy | Partition.RGS_rank() in Python
- Python | Pandas Series.str.partition()
- Python | sympy.partition() method
- Python | SymPy Partition.sort_key() method
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.




