Python String count() with Examples

Posted in /  

Python String count() with Examples
vinaykhatri

Vinay Khatri
Last updated on December 22, 2024

    The Python String is an object and like other objects, it comes with some built-in functions which are known as Python string methods. count() is one of the Python string built-in methods and it accepts a substring and returns the total count of the substring in a given string. The count() method is also supported by Python List, where it returns the total count of an element present in the list.

    The Python String count() method check the string from the start index to end index. And start counting the occurrence of a substring in the given string. By default, the count() method starts searching from index 0 of the given string up to the string’s last element, but this behavior can be changed by passing the optional parameters start and end.

    Python String Count() function syntax

    string.count(substring, start, end)

    The Python string’s count() method can accept 3 parameters substring, start, and end.

    Substring

    It is a string value which number of occurrences we want to count from the given string. It is a mandatory parameter. count() method is case sensitive, the substring which number of occurrences you wish to count should have the same case styling the given string.

    start (optional): It is an optional parameter that represents the index value of the given string from where the searching of substring should begin. end (optional): It is also an optional parameter that represents the end index value of the string where the searching should end.

    Return value of Python String count() method

    The count() method return an integer value. It returns the number of specified substring present in a given string. If the substring is not present in the given string the count() method return 0.

    Python string count() function Examples

    let’s see some example of how the Python String count() method works

    Example 1: Count the occurrent of a substring in a string.

    >>> string = "Hello Tech Geek Welcome to TechGeekBuzz.com"
    >>> substring = "ee"
    >>> string.count(substring)
    2

    The count() method is also supported by Python list

     >>> my_list = [1,2,3,4,4,5,5,6]
    >>> my_list.count(5)
    2

    Example 2:  Count the occurrence of a substring with a different case.

    The count() method is case sensitive, so be careful with the substring which occurrence you want to count.

    >>> string = "Hello Tech Geek Welcome to TechGeekBuzz.com"
    >>> substring = "EE"
    >>> string.count(substring)
    0

    As there is no EE in the string, the count() method return 0

    Example 3: Count the occurrence of a substring with specified start and end index

    By default the count() method starts searching and looking for the substring or character from the start of the string up to its end. But by specifying the start and end parameters, the count() method will only count the occurrence of the substring between the specified start and end index values.

    >>> string = "Hello Tech Geek Welcome to TechGeekBuzz.com"
    >>> substring = "ee"
    >>> string.count(substring, 13, 43 )
    1

    In the above example the count(substring, 13, 45) starts searching the substring in string from the 13 th index value to the 45 th index value.

    Conclusion

    The Python string count() method return the total count of a substring that occurred in a given string. The count() method also works with Python list, where it returns the occurrence of a specific list element. The count() method returns an integer value representing the total count. If the substring is not present in the given string, the count method returns 0.

    People are also reading:

    Leave a Comment on this Post

    0 Comments