.partition()
Published Jun 9, 2021Updated Oct 13, 2023
Contribute to Docs
Searches a string for a given keyword and splits that string into a three part tuple.
Syntax
string.partition("keyword")
Example
The .partition()
method searches for an exact match of the given keyword:
my_string = "Do not go gentle into that good night"my_tuple = my_string.partition("gentle")print(my_tuple)# Output: ('Do not go ', 'gentle', ' into that good night')
No Match Found
If no match is found, .partition()
will return a tuple with the full string, followed by two empty strings
Multiple Matches
If there are multiple matches, the first one will be used:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Skill path
Analyze Data with Python
Learn to analyze and visualize data using Python and statistics.Includes 8 CoursesWith CertificateIntermediate13 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours