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 Program to Add Two Numbers
Generate a String
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Tuple
Python Closures
Python Program to Display Calendar
Introduction to Scientific Programming with Python - Joakim Sundnes
Python Dictionary update()
Java String to InputStream
Python Set isdisjoint()
Python Program to Check Leap Year
Machine Learning with Python for everyone - Mark E.Fenner
Python pass statement
Python Machine Learning Eqution Reference - Sebastian Raschka
APIs in Node.js vs Python - A Comparison
Python dir()
Python String count()
How to get current date and time in Python?
Python Iterators
Deep Learning with Applications Using Python - Navin Kumar Manaswi
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python print()
Python Dictionary values()
Python Dictionary fromkeys()
Python Program to Copy a File
Python String format_map()
Node.js vs Python for Backend Development
Python String isalnum()
Python Program to Check Prime Number
Python frozenset()
Python Program to Compute all the Permutation of the String