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:
Statistical Methods for Machine Learning - Disconver how to Transform data into Knowledge with Pytho...
Python Program to Count the Number of Each Vowel
Format ZonedDateTime to String
Python Program to Create a Countdown Timer
Python Program to Count the Occurrence of an Item in a List
Python Set remove()
Python dir()
Python pass statement
Python String casefold()
Python Operators
Python Package
Python classmethod()
Python Program to Append to a File
Python Program to Solve Quadratic Equation
Python String splitlines()
Python List Comprehension
Python Program to Compute the Power of a Number
Python List reverse()
Python Set add()
Converting String to Stream of chars
Python open()
Python String lower()
Python Variables, Constants and Literals
Python Objects and Classes
Python Directory and Files Management
Python List copy()
Python Program to Check Leap Year
Python len()
Python format()
Python Program to Shuffle Deck of Cards
Map to String Conversion in Java
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...