How to use the count) (method for python – Linux hint


Python’s built-in counting method is very useful when we need to know how many times a certain string appears in a text or how many times an element appears in a tuple or list. By default, this method searches for a specific substring within the entire content of a specific string, but this method can also be used to search for the substring within the specific part of the main string. The use of the tel() method in Python is explained in this article with several examples.


The argument of the counting method(() varies depending on the data type of the variable to which it is applied. Two types of syntax for the tel() method are presented below.

A. string.count (bottom layer, beginning, end)

The above tel() method is used for the type of string data. Here the first argument is mandatory and it contains the value sought in the main sequence. The other two are optional. If only the optional start argument is used, the method starts searching from the start position, and if both optional arguments are used, the subsurface between the start and end positions is searched.

B. tuple.count (search term) or list.count (search term)

The above tel() method is only used for the data type tuple or list and has only one mandatory argument containing the search string. The use of these two types of counting methods is explained in the next part of the article.

Using the tel() method in a string without optional arguments:

The following example defines the main line and the search string in two variables, main_str and search_str. The count() method of this script calculates how many times the value of search_str appears in the total value of main_str. Next, the returned value of this method is printed as a formatted string.

/usr/bin/env python3
# Specify the main line to search
main_str = ‘eat to live, don’t live to eat’
# Specify the search line to search
search_str = ‘live’
# Save the counter value to
count = main_str.count(search_str)

# Print output formatted
(the word %s appears in %s times the following text: n%sn %
(search_str, count, main_str))

A way out:

Here is the value of the main text Eat to Live, not Live to Eat, and the search text is live, which occurs twice in the main text.

How to use the count) (method for python – Linux hint

Using the tel() method in a string with optional arguments:

This example demonstrates the use of optional arguments for the tel() method. The first optional argument is used to determine the starting position of the text to be searched, and the last optional argument is used to determine the position of the text on which the search ends. Continuous text and search text are accepted as input. This script performs three types of searches. In the first search, as in the previous example, the full content of the main text is searched for. The second text search starts at position 20 of the main text. During the third search, the search text starts at position 5 and stops at position 25. The following scenario will thus produce three outputs of the tel(() method based on the arguments.

/usr/bin/env python3
# Specify the main search string, where
mainStr = input

# Specify the search string, where
searchStr = input

# Keep the total number of searches at
= mainStrcount (searchStr)
# Print formatted output
(String ‘%s’), %s times appears in main text % (searchStr, count))

# Keep the total number of searches from position 20 count
= mainStr.count(searchStr,20)
# Print formatted output
print(string ‘%s’, %s times appears in main text after position 20n %
(searchStr, count))

# Stores the total number of values found for calculation from position 5 to 25
count = mainStr.count(searchStr,5,25)
# formatted output
print(string ‘%s’, displayed %s times in the body text after position 5 to
25n % (searchStr, count))

A way out:

Here learning Linux with LinuxHint and becoming a Linux expert is used as the main text and Linux as the research text. The word Linux appears three times in the continuous text, once if you start at position 20, and twice if you start at position 5 to 25.

How to use the count) (method for python – Linux hint

Using the tel(() method in tuples and lists:

The following example shows how to search for a specific element in a tuple and a list At the beginning of the script, the tuple line is recognized and printed. Then the search value is accepted as input. Find and calculate how many times the value appears in the car column. The script displays the result of the calculation with a formatted string. The list of lines is also defined and printed, after which a certain input value is searched for and counted in the list, for example a tuple.

# !/usr/bin/env python3
# identify tuple
nameTuple = (‘John’, ‘Janifer’, ‘Lisa’, ‘John’, ‘Lucy’, ‘john’)
# show tuple
print (nameTuple)
# identify search string, which is searched in the tuple
searchStr = input (Enter the search string for the tuplets)
# Hold the counter
count = nameTuplecount(searchStr)
# Print the formatted output
print(The string ‘%s’, appears %s times in tuple % (searchStr, count))

# List definition
fruitList = [‘banana’, ‘mango’, ‘orange’, ‘grape’, ‘mango’, ‘banana’] # List output

searchStr = input (search for listn input)
# Save counter value
count = fruitList.count(searchStr)
# Print formatted output
print(String ‘%s’, %s times appears in the % list (searchStr, count))

A way out:

The following output appears if you list John as search text on tuple and Mango as search text.

How to use the count) (method for python – Linux hint


The different ways to use the count() method on Python are presented in this article to help Python users use this method in their Python script.write a python program to count the number of lines in a text file.,python count characters in file

Latest Posts