find()

my_str="Welcome to plus2net.com"
print(my_str.find('co'))      # Output is 3
print(my_str.find('co',4))    # Output is 20 
print(my_str.find('co',4,18)) # Output is -1
my_str.find(search_string,start,end))
search_string : String to be checked inside the main string.
start: Optional , search to start from this position
end : Optional , serch to end from this position.

Check if the search string is within our main string, if found then the position of matching string is returned. If not found then -1 is returned.

Case sensitive

find() is a case sensitive search
my_str="Welcome to plus2net.com"
print(my_str.find('CO'))      # Output is -1

Case in-sensitive serch

We can use lower() to change the case of both strings to lower and then use find()
my_str="Welcome to plus2net.com".lower()
print(my_str.find('CO'.lower()))      # Output is 3
Using if - else
my_str="Welcome to plus2net.com".lower()

if my_str.find('CO'.lower())<0:
    print('It is NOT there')
else:
    print('It is  there')
Output
It is  there
Regular Expression can be used to search string

index() and find()

We can use index() method also to search and find the position but find() method returns -1 if not found but index() method raise an exception if not found.

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