

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String partition() in Python
In this tutorial, we are going to learn about the partition method of string.
The method partition() takes one argument i.e.., separator and returns a tuple that contains substrings they are substring before separator, separator, and substring after separator.
The partition() method takes the first occurrence of the separator. Follow the below steps to write the code.
- Initialize a string.
- Print the partitioned tuple using partition(separator) method.
- You can pass the any separator you want.
Example
# initializing a string string = "Tutorialspoint is a great place to learn Python" # partitioning the string with a space print(string.partition(' '))
Output
If you run the above code, then you will get the following result.
('Tutorialspoint', ' ', 'is a great place to learn Python')
It will return a tuple with total string and two empty strings if the given separator is not found in the string. Let's see an example.
Example
# initializing a string string = "Tutorialspoint is a great place to learn Python" # partitioning the string with a space print(string.partition('tutorialspoint'))
Output
If you execute the above program, then you will get the following result.
('Tutorialspoint is a great place to learn Python', '', '')
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Questions & Answers
- Array Partition I in Python
- Program to partition two strings such that each partition forms anagram in Python
- Partition Array for Maximum Sum in Python
- Program to partition color list in Python
- Partition problem
- Partition List in C++
- Partition Labels in C++
- Partition Problem in C++
- Equal Tree Partition in C++
- Partition Array Into Three Parts With Equal Sum in Python
- Program to find partition array into disjoint intervals in Python
- Partition Equal Subset Sum in C++
- Partition Array into Disjoint Intervals in C++
- Find a partition point in array in C++
- Check if it possible to partition in k subarrays with equal sum in Python
Advertisements