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 List clear()
Python Program to Merge Two Dictionaries
Python hasattr()
Converting a List to String in Java
Python Objects and Classes
Python Program to Print Output Without a Newline
Python float()
Python Keywords and Identifiers
APIs in Node.js vs Python - A Comparison
Python Inheritance
Python memoryview()
Python Program to Get the Full Path of the Current Working Directory
Java InputStream to String
Python Program to Find Numbers Divisible by Another Number
Python sorted()
Case-Insensitive String Matching in Java
Split a String in Java
Python Program to Find the Largest Among Three Numbers
Python Dictionary copy()
Converting a Stack Trace to a String in Java
Python Program to Print all Prime Numbers in an Interval
Python Program to Print Hello world!
Python list()
Python Shallow Copy and Deep Copy
Deep Learning with Python - Francois Cholletf
Python String replace()
Python Function Arguments
Python Program to Get Line Count of a File
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python RegEx
Python __import__()
Python String capitalize()