my_str.partition(search_char)
search_char: Used to search the input string my_strmy_str='Welcome to Plus2net'
output=my_str.partition('to')
print(output)
Output is here
('Welcome ', 'to', ' Plus2net')
We can add one more line to display the last element
print(output[2])
Output
plus2net
If search string is not found then input string is returned as first element of the tuple and rest two elements will be blank.
my_str='Welcome to Plus2net'
output=my_str.partition('*')
print(output)
Output
('Welcome to Plus2net', '', '')
By using partition() method you can separate domain part and userid part of an email address.
my_str='userid@example.com'
output=my_str.partition('@')
print(output)
print('Userid :',output[0])
print('Domain :',output[2])
Output
('userid', '@', 'example.com')
Userid : userid
Domain : example.com
All String methods
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.