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 repr()
Python Program to Display Fibonacci Sequence Using Recursion
Python Tuple index()
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Converting a Stack Trace to a String in Java
Python dict()
Python list()
Python staticmethod()
Check If a String Is Numeric in Java
Python String isalpha()
Python isinstance()
Adding a Newline Character to a String in Java
Python Directory and Files Management
Python getattr()
Python String replace()
Python __import__()
Converting a List to String in Java
Python Set issubset()
Python Decorators
Deep Learning with Python - Francois Cholletf
Python Program to Find the Size (Resolution) of a Image
Python slice()
Java Program to Permute All Letters of an Input String
Python Dictionary popitem()
Python Program to Differentiate Between type() and isinstance()
Python String swapcase()
Python Data Types
Python bytearray()
Python Multiple Inheritance
Python String lstrip()
Java – String to Reader
Python Keywords and Identifiers