How do you count text in python?

The count() is a built-in function in Python. It will return the total count of a given element in a string. The counting begins from the start of the string till the end. It is also possible to specify the start and end index from where you want the search to begin.

In this Python tutorial, you will learn:

  • Python count
  • The syntax for PythonString Count()
  • Example 1: Count Method on a String
  • Example 2: Count occurrence of a character in a given string
  • Example 3: Count occurrence of substring in a given string

The syntax for PythonString Count()

Python count function syntax:

string.count(char or substring, start, end)

Parameters of Python Syntax

  • Char or substring: You can specify a single character or substring you are wants to search in the given string. It will return you the count of the character or substring in the given string.
  • start : (optional) It indicates the start index from where the search will begin. If not given, it will start from 0. For example, you want to search for a character from the middle of the string. You can give the start value to your count function.
  • end: (optional) It indicates the end index where the search ends. If not given, it will search till the end of the list or string given. For example, you don’t want to scan the entire string and limit the search till a specific point you can give the value to end in your count function, and the count will take care of searching till that point.

ReturnValue

The count() method will return an integer value, i.e., the count of the given element from the given string. It returns a 0 if the value is not found in the given string.

Example 1: Count Method on a String

The following example shows the working of count() function on a string.

str1 = "Hello World"
str_count1 = str1.count('o')  # counting the character “o” in the givenstring
print("The count of 'o' is", str_count1)

str_count2 = str1.count('o', 0,5)
print("The count of 'o' usingstart/end is", str_count2)

Output:

The count of 'o' is 2
The count of 'o' usingstart/end is 1

Example 2: Count occurrence of a character in a given string

The following example shows the occurrence of a character in a given string as well as in by using the start/end index.

str1 = "Welcome to Guru99 Tutorials!"
str_count1 = str1.count('u')  # counting the character “u” in the given string
print("The count of 'u' is", str_count1)

str_count2 = str1.count('u', 6,15)
print("The count of 'u' usingstart/end is", str_count2)

Output:

The count of 'u' is 3
The count of 'u' usingstart/end is 2

Example 3: Count occurrence of substring in a given string

Following example shows the occurrence of substring in a givenstring as well as usingstart/endindex.

str1 = "Welcome to Guru99 - Free Training Tutorials and Videos for IT Courses"
str_count1 = str1.count('to') # counting the substring “to” in the givenstring
print("The count of 'to' is", str_count1)
str_count2 = str1.count('to', 6,15)
print("The count of 'to' usingstart/end is", str_count2)

Output:

The count of 'to' is 2
The count of 'to' usingstart/end is 1

Summary:

  • The count() is a built-in function in Python. It will return you the count of a given element in a list or a string.
  • In the case of a string, the counting begins from the start of the string till the end. It is also possible to specify the start and end index from where you want the search to begin.
  • The count() method returns an integer value.

❮ String Methods


Example

Return the number of times the value "apple" appears in the string:

txt = "I love apples, apple are my favorite fruit"

x = txt.count("apple")

print(x)

Try it Yourself »


Definition and Usage

The count() method returns the number of times a specified value appears in the string.


Syntax

string.count(value, start, end)

Parameter Values

ParameterDescription
value Required. A String. The string to value to search for
start Optional. An Integer. The position to start the search. Default is 0
end Optional. An Integer. The position to end the search. Default is the end of the string

More Examples

Example

Search from position 10 to 24:

txt = "I love apples, apple are my favorite fruit"

x = txt.count("apple", 10, 24)

print(x)

Try it Yourself »


❮ String Methods


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Python String count() function is an inbuilt function in python programming language that returns the number of occurrences of a substring in the given string.

    Syntax: 

    string.count(substring, start=…, end=…)

    Parameters: 

    • The count() function has one compulsory and two optional parameters. 
      • Mandatory parameter: 
        • substring – string whose count is to be found.
      • Optional Parameters: 
        • start (Optional) – starting index within the string where the search starts. 
        • end (Optional) – ending index within the string where the search ends.

    Return Value:

    count() method returns an integer that denotes number of times a substring occurs in a given string. 

    Example 1: Implementation of the count() method without optional parameters

    Python3

    string = "geeks for geeks" 

    print(string.count("geeks"))

    Output: 

    2

    Example 2: Implementation of the count() method using optional parameters

    Python3

    string = "geeks for geeks" 

    print(string.count("geeks", 0, 5))

    print(string.count("geeks", 0, 15))

    Output: 

    1
    2

    How do you count words in Python?

    Python Code: def word_count(str): counts = dict() words = str. split() for word in words: if word in counts: counts[word] += 1 else: counts[word] = 1 return counts print( word_count('the quick brown fox jumps over the lazy dog. '))

    Can you count a string in Python?

    Python String count() function is an inbuilt function in python programming language that returns the number of occurrences of a substring in the given string. Parameters: The count() function has one compulsory and two optional parameters.

    How do you count letters in Python?

    In Python, you can get the length of a string str (= number of characters) with the built-in function len() .

    How do you count text in a list in Python?

    The count() is a built-in function in Python. It will return you the count of a given element in a list or a string. In the case of a list, the element to be counted needs to be given to the count() function, and it will return the count of the element. The count() method returns an integer value.

    Tải thêm tài liệu liên quan đến bài viết How do you count text in python?