Finding an exact substring in a string in Python

Finding an exact substring in a string in Python

If you want to find an exact substring within a string in Python, you can use the in keyword or the find method. Here's an example using both approaches:

  1. Using the in keyword:
main_string = "Hello, world! This is a sample string." substring = "world" if substring in main_string: print(f"The substring '{substring}' was found.") else: print(f"The substring '{substring}' was not found.") 
  1. Using the find method:
main_string = "Hello, world! This is a sample string." substring = "world" index = main_string.find(substring) if index != -1: print(f"The substring '{substring}' was found at index {index}.") else: print(f"The substring '{substring}' was not found.") 

In both examples, the substring variable represents the exact substring you want to find within the main_string. If the substring is found, the program prints a message indicating its presence along with additional information such as the index if using the find method.

Choose the approach that best fits your needs. The in keyword provides a simple boolean result, while the find method additionally returns the index of the first occurrence of the substring or -1 if not found.

Examples

  1. Python find exact substring using equality check

    • Description: Find an exact substring using simple equality check.
    main_string = "YourMainString" substring = "YourSubstring" if substring == main_string: print("Exact substring found.") 
  2. Python find exact substring using find method

    • Description: Use the find method to find the index of an exact substring.
    main_string = "YourMainString" substring = "YourSubstring" if main_string.find(substring) != -1: print("Exact substring found.") 
  3. Python find exact substring using in keyword

    • Description: Check if an exact substring is present using the in keyword.
    main_string = "YourMainString" substring = "YourSubstring" if substring in main_string: print("Exact substring found.") 
  4. Python find exact substring at the beginning

    • Description: Check if an exact substring is at the beginning of a string.
    main_string = "YourMainString" substring = "YourSubstring" if main_string.startswith(substring): print("Exact substring found at the beginning.") 
  5. Python find exact substring at the end

    • Description: Check if an exact substring is at the end of a string.
    main_string = "YourMainString" substring = "YourSubstring" if main_string.endswith(substring): print("Exact substring found at the end.") 
  6. Python find exact substring using regex

    • Description: Use the re module to find an exact substring with regex.
    import re main_string = "YourMainString" substring = "YourSubstring" if re.search(fr'\b{re.escape(substring)}\b', main_string): print("Exact substring found.") 
  7. Python find exact substring case-insensitive

    • Description: Find an exact substring case-insensitively.
    main_string = "YourMainString" substring = "YourSubstring" if substring.lower() in main_string.lower(): print("Exact substring found (case-insensitive).") 
  8. Python find multiple occurrences of an exact substring

    • Description: Find all occurrences of an exact substring in a string.
    main_string = "YourMainString YourSubstring YourSubstring" substring = "YourSubstring" occurrences = [i for i in range(len(main_string)) if main_string.startswith(substring, i)] print("Occurrences at positions:", occurrences) 
  9. Python find exact substring using str.index method

    • Description: Use the index method to find the index of an exact substring.
    main_string = "YourMainString" substring = "YourSubstring" try: index = main_string.index(substring) print(f"Exact substring found at index {index}.") except ValueError: print("Substring not found.") 
  10. Python find exact substring with specified boundaries

    • Description: Find an exact substring with specified start and end boundaries.
    main_string = "YourMainString YourSubstring YourMainString" substring = "YourSubstring" start_boundary = 15 end_boundary = 30 if main_string.find(substring, start_boundary, end_boundary) != -1: print("Exact substring found within specified boundaries.") 

More Tags

multimarkdown xamarin.forms aforge uppercase autohotkey amp-html xml ssrs-2008 eonasdan-datetimepicker weak-entity

More Programming Questions

More Chemical reactions Calculators

More Various Measurements Units Calculators

More Housing Building Calculators

More Electrochemistry Calculators