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 type()
Python Program to Illustrate Different Set Operations
Python Program to Display Calendar
Python List copy()
Python Numbers, Type Conversion and Mathematics
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python bytearray()
Converting a Stack Trace to a String in Java
Python Program to Find Numbers Divisible by Another Number
Python Program to Append to a File
Python Set discard()
Python String format()
Python any()
Python next()
Python File I/O Operation
Python vars()
Python Set union()
Python Program to Access Index of a List Using for Loop
Deep Learning in Python - LazyProgrammer
Deep Learning with Python - Francois Chollet
Python zip()
Java – Reader to String
Python Program to Check Leap Year
Python String split()
Python Program to Make a Flattened List from Nested List
Python Dictionary get()
Python id()
Python @property decorator
How to get current date and time in Python?
Python Package
Python Program to Add Two Matrices
Python delattr()