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 Reverse a Number
Python List
Python Program to Sort Words in Alphabetic Order
Python classmethod()
Python Decorators
Python Tuple
Python Deep Learning Cookbook - Indra den Bakker
Python String center()
Building Chatbots with Python Using Natural Language Processing and Machine Learning - Sumit Raj
Python round()
Python String isdigit()
Python Program to Measure the Elapsed Time in Python
Python all()
Python Dictionary items()
Python Program to Get the Class Name of an Instance
Python pass statement
Python List copy()
Python Program to Print Hello world!
Python String rjust()
Java InputStream to String
Java String to InputStream
Python Program to Catch Multiple Exceptions in One Line
Python Dictionary keys()
Python Closures
Python List index()
Python Dictionary popitem()
Map to String Conversion in Java
Python Set add()
Python frozenset()
Python Set intersection_update()
Python hash()
Python Program to Safely Create a Nested Directory