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 copy()
Case-Insensitive String Matching in Java
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Convert String to int or Integer in Java
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Solve Quadratic Equation
Python Program to Convert Decimal to Binary Using Recursion
Python Program to Display the multiplication Table
Python staticmethod()
Python bin()
Introduction to Machine Learning with Python - Andreas C.Muller & Sarah Guido
Python type()
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Shallow Copy and Deep Copy
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python Program to Capitalize the First Character of a String
Python Program to Merge Two Dictionaries
Python String isprintable()
Python globals()
Python Program to Convert Decimal to Binary, Octal and Hexadecimal
Most commonly used String methods in Java
Python Program to Check if a Key is Already Present in a Dictionary
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Python Tuple
Reading an HTTP Response Body as a String in Java
Python Operators
Python Program to Convert String to Datetime
Python map()
Python Tuple count()
Python String replace()
Python Set symmetric_difference_update()
Python Program to Return Multiple Values From a Function