Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll learn about the find()
method in Python, which is a string method that searches for a specified substring within a string and returns the index of the first occurrence. If the substring is not found, the method returns -1.
The find()
method takes the following arguments:
sub
(required): The substring to search for within the string.start
(optional): The starting index from which to search for the substring. By default, it is 0.end
(optional): The ending index up to which to search for the substring. By default, it is the length of the string.Example 1: Basic usage of the find()
method
text = "Hello, world! Welcome to the world of Python." substring = "world" index = text.find(substring) print(index) # Output: 7
Example 2: Using start
and end
arguments
text = "Hello, world! Welcome to the world of Python." substring = "world" index = text.find(substring, 10) # Start searching from index 10 print(index) # Output: 28
text = "Hello, world! Welcome to the world of Python." substring = "world" index = text.find(substring, 0, 10) # Search between index 0 and 10 print(index) # Output: 7
Example 3: Substring not found
text = "Hello, world! Welcome to the world of Python." substring = "Java" index = text.find(substring) print(index) # Output: -1
Note that the find()
method is case-sensitive. If you want to perform a case-insensitive search, you can convert both the text and the substring to lowercase (or uppercase) before using the find()
method.
text = "Hello, world! Welcome to the World of Python." substring = "WORLD" index = text.lower().find(substring.lower()) print(index) # Output: 28
In summary, the find()
method is used to search for a specified substring within a string and returns the index of the first occurrence. If the substring is not found, it returns -1. You can also specify a start and end index to search within a specific range.
How to Use find()
to Detect Substring in Python:
find()
method returns the index of the first occurrence of a substring in a string. If not found, it returns -1.# Example sentence = "Python is powerful" index = sentence.find("is")
Checking for Substring Presence with find()
in Python:
find()
to check if a substring is present in a string.# Example sentence = "Python programming" if sentence.find("java") != -1: print("Substring found") else: print("Substring not found")
Find Substring Index in a String with Python find()
:
find()
.# Example sentence = "Python is versatile" index_python = sentence.find("Python")
Using find()
to Search for a Substring Occurrence in Python:
find()
.# Example sentence = "Python programming is fun. Python is powerful." index_second_python = sentence.find("Python", sentence.find("Python") + 1)
Handling Missing Substrings with find()
in Python:
# Example sentence = "Python is amazing" substring = "Java" index = sentence.find(substring) if index != -1: print(f"Substring found at index {index}") else: print(f"Substring '{substring}' not found")
Case-Insensitive Substring Search with find()
in Python:
lower()
.# Example sentence = "Python is CASE-INSENSITIVE" substring = "case-insensitive" index = sentence.lower().find(substring.lower())
Python find()
vs index()
for Substring Detection:
find()
returns -1 for missing substrings, index()
raises a ValueError
.# Example with find() sentence = "Python is great" index_find = sentence.find("Java") # Returns -1 if not found # Example with index() try: index_index = sentence.index("Java") # Raises ValueError if not found except ValueError: index_index = -1
Detecting Multiple Substring Occurrences with find()
in Python:
find()
.# Example sentence = "Python is easy. Python is versatile. Python is powerful." substring = "Python" start_index = 0 while True: index = sentence.find(substring, start_index) if index == -1: break print(f"Found at index: {index}") start_index = index + len(substring)