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 Find All File with .txt Extension Present Inside a Directory
Python Dictionary keys()
Python Program to Add Two Numbers
Python Program to Convert Kilometers to Miles
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Print Hello world!
Python String count()
Python String ljust()
Python Program to Check Leap Year
Python Namespace and Scope
Python Dictionary
Python Program to Capitalize the First Character of a String
Python Program to Count the Occurrence of an Item in a List
Python Machine Learning - Sebastian Raschka
Python delattr()
Python RegEx
Python Program to Convert String to Datetime
Python String endswith()
Python setattr()
Python *args and **kwargs
Python String isidentifier()
Python Program to Get a Substring of a String
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Object Oriented Programming
Python Program to Find Numbers Divisible by Another Number
Python filter()
Convert String to int or Integer in Java
Map to String Conversion in Java
Python break and continue
Encode a String to UTF-8 in Java
Python strptime()
Python Directory and Files Management