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 Program to Convert Decimal to Binary Using Recursion
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Python String format()
Python Functions
Python memoryview()
Python oct()
Python Custom Exceptions
Python Tuple index()
Python Keywords and Identifiers
Python Dictionary clear()
Python String rstrip()
Python Program to Get Line Count of a File
Python divmod()
Python format()
Python Program to Merge Two Dictionaries
Python len()
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Count the Number of Each Vowel
Python String isnumeric()
Python Program to Count the Number of Digits Present In a Number
Python String isspace()
Python String title()
Python String count()
Python Set issubset()
Python Program to Check Whether a String is Palindrome or Not
Python Namespace and Scope
Python getattr()
Python frozenset()
Java – Generate Random String
Deep Learning from Scratch - Building with Python form First Principles - Seth Weidman
Python all()
Python Dictionary items()