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
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.