Python String find() Method

The find() method is a string method that returns the lowest index of a substring within a string if it is found. If not, it returns -1.

For example:


s = "Hello world"
print(s.find("world"))
# prints 6
print(s.find("Python")) # prints -1

You can also specify the start and end positions to search within a slice of the string.

For example:

s = "Hello world"
print(s.find("o", 5))
# prints 7
print(s.find("o", 5, 8)) # prints -1

If you have any questions about this code, you can drop a line in comment.

 

Comments

Popular posts from this blog

Python float() Built in Function

Python String encode() Method

Python getattr() Built in Function

Python Use Get Method Over Square Brackets

Python exec() Built in Function