Table of Contents
The partition() method splits the string at the first occurrence of the argument string and returns a tuple containing the part the before separator, argument string and the part after the separator.
The syntax of partition() is:
string.partition(separator)
1. partition() Parameters()
The partition() method takes a string parameter separator that separates the string at the first occurrence of it.
2. Return Value from partition()
The partition method returns a 3-tuple containing:
- the part before the separator, separator parameter, and the part after the separator if the separator parameter is found in the string
- the string itself and two empty strings if the separator parameter is not found
3. Example: How partition() works?
string = "Python is fun"
# 'is' separator is found
print(string.partition('is '))
# 'not' separator is not found
print(string.partition('not '))
string = "Python is fun, isn't it"
# splits at first occurence of 'is'
print(string.partition('is'))
Output
('Python ', 'is ', 'fun')
('Python is fun', '', '')
('Python ', 'is', " fun, isn't it")
Related posts:
Python String endswith()
String Set Queries
Python List pop()
Python Input, Output and Import
Python zip()
Python tuple()
Python id()
Python Program to Slice Lists
Python String lstrip()
Python Set update()
How to Get Started With Python?
Python String rpartition()
Python Dictionary pop()
Python Program to Safely Create a Nested Directory
Python Program to Create a Long Multiline String
Python String isdigit()
Python Set issuperset()
Python Program to Swap Two Variables
Python object()
Python Set issubset()
Python String rstrip()
Python Program to Find the Sum of Natural Numbers
Python Program to Get the Class Name of an Instance
Case-Insensitive String Matching in Java
Python String rsplit()
Python Set pop()
Python Data Analytics with Pandas, NumPy and Matplotlib - Fabio Nelli
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Iterate Over Dictionaries Using for Loop
Python Program to Compute the Power of a Number
Python Program to Get Line Count of a File
Python pass statement