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 dict()
Python Set discard()
Python Program to Display Calendar
Python compile()
Python Matrices and NumPy Arrays
Python Program to Add Two Matrices
Python Program to Add Two Numbers
JavaScript Methods of RegExp and String
Python Program to Get the Last Element of the List
Python Package
Python if...else Statement
Python Program to Make a Flattened List from Nested List
Python String expandtabs()
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python Program to Count the Number of Occurrence of a Character in String
Python bytearray()
Python Set issuperset()
Python Program to Check Armstrong Number
Case-Insensitive String Matching in Java
Adding a Newline Character to a String in Java
Python Machine Learning Third Edition - Sebastian Raschka & Vahid Mirjalili
Python Machine Learning Cookbook - Practical solutions from preprocessing to Deep Learning - Chris A...
Python sleep()
Python Program to Find Numbers Divisible by Another Number
Python dir()
Python Get Current time
Python String swapcase()
Python filter()
Python String isupper()
How to Get Started With Python?
Python List clear()
Python Set clear()