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 zip()
Introduction to Scientific Programming with Python - Joakim Sundnes
Python String istitle()
Python Program to Count the Number of Digits Present In a Number
Python sleep()
Python Dictionary values()
Python divmod()
Python Program to Create a Countdown Timer
Python String casefold()
Python Machine Learning - Sebastian Raschka
Python Object Oriented Programming
Python String maketrans()
Python Functions
Python Program to Get a Substring of a String
Python Program to Find Armstrong Number in an Interval
Python Program to Convert Decimal to Binary Using Recursion
Machine Learning with Python for everyone - Mark E.Fenner
Python Program to Check Leap Year
Python String startswith()
Python max()
How to get current date and time in Python?
Python enumerate()
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python input()
Python Set remove()
Python dict()
Python Program to Print Hello world!
Python Operators
Generate a String
Python Program to Safely Create a Nested Directory
Python String isnumeric()
Python open()