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 Set difference()
Python Errors and Built-in Exceptions
Python delattr()
Python Program to Shuffle Deck of Cards
Python tuple()
Python Machine Learning - Sebastian Raschka
Python Program Read a File Line by Line Into a List
Python Program to Check Prime Number
Python String lower()
Python Program to Compute all the Permutation of the String
Python locals()
Python String zfill()
Python filter()
Python Program to Display the multiplication Table
Python String index()
Python Decorators
Python Get Current time
Python String rjust()
Python Program to Randomly Select an Element From the List
Intelligent Projects Using Python - Santanu Pattanayak
Python Dictionary clear()
Python String title()
Python Matrices and NumPy Arrays
Python Program to Print the Fibonacci sequence
Python frozenset()
Python Dictionary update()
Python Generators
Converting a Stack Trace to a String in Java
Python Program to Check Whether a String is Palindrome or Not
Python List index()
Python Strings
Python int()