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 Multiple Inheritance
Python List reverse()
Python Program to Split a List Into Evenly Sized Chunks
Python Sets
Python Machine Learning - Sebastian Raschka
Python Deeper Insights into Machine Learning - Sebastian Raschka & David Julian & John Hearty
Python Program to Parse a String to a Float or Int
Python Set clear()
Python Set discard()
Python break and continue
String Initialization in Java
Node.js vs Python for Backend Development
Python String rfind()
Python eval()
Python range()
Python dict()
Python locals()
Python Recursion
Python Program to Display Calendar
Python String capitalize()
Python property()
Python Program to Access Index of a List Using for Loop
Python Variables, Constants and Literals
Python slice()
Deep Learning with Python - Francois Chollet
Python Statement, Indentation and Comments
Python Closures
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python Program to Print all Prime Numbers in an Interval
Python Program to Copy a File
Python classmethod()
Python Program to Find Hash of File