Python String find() Method with Examples

Posted in /  

Python String find() Method with Examples
vinaykhatri

Vinay Khatri
Last updated on October 31, 2024

    String in Python is an inbuilt object that is represented using str keyword. Like other objects string support some built methods and find() is one of those. The Python string find(sub) method or function accepts a substring value and return its first occurring index value in the given string. If the substring value is not present in the given string, the Python string find() function return -1 value.

    Note: In Python, everything is an object, including Python data types. The inbuilt functions associated with data type or structures are known as methods. You can also interchangeably use the term functions and methods. But it is suggested to call the object’s functions as methods .

    Python String find() Method Syntax and Parameter

    string.find(substring, start, end)

    The string find() method can accept three parameters

    • substring
    • start
    • end

    Substring: It is a string value which index we want to find out from the given string.

    start (optional): It is an optional parameter and by default, its value is 0. It is an integer number that represent the starting index value of a given string from where the searching of substring should begin.

    end (optional): It is also an optional parameter and by default, its value is equal to the length of the given string. It is an integer value that represents the end index value for the given string up to which the searching for substring should limit. The Python String find() method only return the lowest index value of the given string where it found the sub string. If the given string has multiple substrings, the method will only return the first occurrence.

    How to Use Python String find() method Example

    With the help of string find() method we can point out the substring starting index value within a given string. The string find() method come very useful during string analysis and manipulation.

    Python string find() Examples

    Example 1

    >>> string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    >>> sub = "Tech"
    >>> print("The substring", sub, "is at", string.find(sub))
    The substring Tech is at 6

    In the above example, you can see that the starting index value of Tech in string is 6 , that’s why the find method returns 6 as an output.

    Example 2

    >>> string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    >>> sub = "python"
    >>> print("The substring", sub, "is at", string.find(sub))
    
    The substring python is at -1

    As sub python is not present in the string, the find() method return -1 .

    Example 3

    >>> string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    >>> sub = "tech"
    >>> print("The substring", sub, "is at", string.find(sub))
    The substring tech is at -1

    The find() method matches the substring each character with the string and return the exact index position for the first occurrence. The find() method is case sensitive, the case of substring should also match the case of given string else the find() method return -1 as no result found.

    Example 4

    Python string find(start) method with start parameter

    >>> string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    >>> sub = "Tech"
    >>> print("The substring", sub, "is at", string.find(sub,7))
    The substring Tech is at 27

    In this example the find(sub, 7) , method starts searching the Tech substring from the 7 th index value of the string. That’s why it ignores all the first 6 characters of the string .

    Example 5

    Python string find(start,end) method with start and end parameters

    >>> string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    >>> sub = "Geek"
    >>> print("The substring", sub, "is at", string.find(sub,0, 14))
    
    The substring Geek is at -1

    In this example the find(sub, 0,14) , method starts searching the Geek substring from the 0 th index value up to 14 of the string. Although the first occurrence of Geek start at 11 but it ends at 15, and we have specified the end parameter to 14 so the find() method is not able to find Geek and return -1 .

    Python String rfind() method

    The Python string rfind() function is similar to the Python string find() function, the only difference is the find() return the first occurrence index value and rfind() return the last occurrence index value of substring. Apart from this difference both find() and rfind() methods are pretty same. Both return -1 if the substring is not present in the given string.

    Python rfind() syntax

    string.rfind(substring, start, end)

    Python rfind() method Example:

    Example 1

    >>> string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    >>> sub = "Geek"
    >>> print("The last substring", sub, "is at", string.rfind(sub))
    
    The last substring Geek is at 31

    The last occurrence of Geek start from 31 st index position in TechGeekBuzz.com . That’s why rfind() method return 31 , instead of 11 which is the first Geek substring position.

    Example 2

    >>> string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    >>> sub = "geek"
    >>> print("The last substring", sub, "is at", string.rfind(sub))
    The last substring geek is at -1

    Similar to the find() method rfind() return -1 if it does not find the substring in the given string.

    Python string index() method

    The Python string index() method also returns the index value of the first occurrence of the substring. But unlike find() method the index() method throws an exception or error if the sub string is not present in the given string.

    Python string index() method syntax

    string.index(substring, start,end)

    Python string index() method Examples

    Example 1

    string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    
    sub = "Tech"
    print("The first occurrence of", sub, "is at", string.index(sub))
    print("The first occurrence of", sub, "is at", string.find(sub))

    Output

    The first occurrence of Tech is at 6
    The first occurrence of Tech is at 6

    Example 2

    string = "Hello Tech Geek WelCome to TechGeekBuzz.com"
    sub = "tech"
    print("The first occurance of", sub, "is at", string.index(sub))

    Output

    print("The first occurance of", sub, "is at", string.index(sub))
    ValueError: substring not found

    Conclusion

    The Python string find() method returns the first occurrence index value of the substring. It can accept three parameters substring, start and end. Similar to the find() method, Python string also supports rfind() method which returns the last occurrence index value of the substring in a given string.

    The Python string index() method also returns the first occurrence index value of substring, but if the substring is not present in the given string the index() method throws the ValueError.

    People are also reading:

    Leave a Comment on this Post

    0 Comments