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)

Python find() method: detect whether a string contains a substring

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.

  1. How to Use find() to Detect Substring in Python:

    • The 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")
    
  2. Checking for Substring Presence with find() in Python:

    • Use 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")
    
  3. Find Substring Index in a String with Python find():

    • Retrieve the index of the first occurrence of a substring using find().
    # Example
    sentence = "Python is versatile"
    index_python = sentence.find("Python")
    
  4. Using find() to Search for a Substring Occurrence in Python:

    • Search for a specific substring occurrence using find().
    # Example
    sentence = "Python programming is fun. Python is powerful."
    index_second_python = sentence.find("Python", sentence.find("Python") + 1)
    
  5. Handling Missing Substrings with find() in Python:

    • Check for the presence of a substring and handle missing substrings.
    # 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")
    
  6. Case-Insensitive Substring Search with find() in Python:

    • Perform a case-insensitive substring search using lower().
    # Example
    sentence = "Python is CASE-INSENSITIVE"
    substring = "case-insensitive"
    index = sentence.lower().find(substring.lower())
    
  7. Python find() vs index() for Substring Detection:

    • While 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
    
  8. Detecting Multiple Substring Occurrences with find() in Python:

    • Iterate to find multiple occurrences of a substring using 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)