partition()

Break a string into three parts using the input search string ( search_char)
my_str.partition(search_char)
search_char: Used to search the input string my_str
partition() returns a three element tuple by breaking the string using input search string. Three parts are here.

1. Left part of the matched string
2. The searched string
3. Right part of the matched string

my_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


Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer