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 range()
Python Package
Python Dictionary get()
Python for Loop
Python Program to Count the Occurrence of an Item in a List
Python Program to Convert Celsius To Fahrenheit
Python String format_map()
Python Input, Output and Import
Python Program to Print all Prime Numbers in an Interval
Java – String to Reader
Python String rfind()
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python dir()
Python Program to Create a Long Multiline String
Python List clear()
Python List remove()
Python Program to Delete an Element From a Dictionary
Intelligent Projects Using Python - Santanu Pattanayak
Python timestamp to datetime and vice-versa
Python Program to Remove Duplicate Element From a List
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python filter()
JavaScript Methods of RegExp and String
Split a String in Java
Jackson – Marshall String to JsonNode
Python String strip()
Python Program to Sort a Dictionary by Value
Python String isspace()
Python String isupper()
Python bool()
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python @property decorator