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 append()
Python Program to Check Prime Number
Node.js vs Python for Backend Development
Python enumerate()
Python String strip()
Python Program to Get the Class Name of an Instance
Python Program to Check If a List is Empty
Python strptime()
Python Program to Remove Punctuations From a String
Python String zfill()
Python String title()
Deep Learning with Python - Francois Cholletf
Case-Insensitive String Matching in Java
Converting a Stack Trace to a String in Java
Python Program to Make a Flattened List from Nested List
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python String rsplit()
Python String format_map()
Converting a Stack Trace to a String in Java
Python String replace()
Python list()
Python String rfind()
Python Set intersection_update()
Python List pop()
Python __import__()
Python iter()
Deep Learning in Python - LazyProgrammer
Python Directory and Files Management
Python String maketrans()
Python Data Types
Python dir()
Python Program to Count the Occurrence of an Item in a List