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 Operator Overloading
Python String isidentifier()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Python List insert()
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Check Leap Year
Python Recursion
Python Anonymous / Lambda Function
Python float()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Tuple index()
Python sleep()
Python locals()
Python Program to Print the Fibonacci sequence
Python Global Keyword
Python del Statement
Python Strings
Python String rfind()
Converting a List to String in Java
Python while Loop
Python Program to Swap Two Variables
Python Generators
Array to String Conversions
Deep Learning with Python - Francois Cholletf
Python Program to Get Line Count of a File
Python List reverse()
Python Program to Check If Two Strings are Anagram
APIs in Node.js vs Python - A Comparison
String Processing with Apache Commons Lang 3
Converting a Stack Trace to a String in Java
Python Program to Illustrate Different Set Operations
Python Program to Print all Prime Numbers in an Interval