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 timestamp to datetime and vice-versa
Converting a List to String in Java
Python Tuple count()
Python Program to Find the Factorial of a Number
Python int()
Python Program to Parse a String to a Float or Int
Python Program to Print Hello world!
Python len()
Python Operator Overloading
Python Program to Sort Words in Alphabetic Order
Python Program to Merge Two Dictionaries
Python String lstrip()
Python Program to Convert Celsius To Fahrenheit
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python String split()
Python String splitlines()
Python dir()
Python id()
Check If a String Is Numeric in Java
Python list()
Python Keywords and Identifiers
Python Program to Print the Fibonacci sequence
Python Program to Print Output Without a Newline
Converting String to Stream of chars
Python reversed()
Python classmethod()
Python Dictionary clear()
Python sorted()
Python Program to Check Whether a String is Palindrome or Not
Python Program to Make a Flattened List from Nested List
Python String rfind()
Python open()