Python’s built-in count function can be called on different data types and is used to count occurrences of a particular item passed as a parameter.
Possible applications¶
You can use Python count for various purposes. This function is particularly suitable for frequency analyzes. It can be used for example to count the number of specific letters or words in a text. Python count is also useful for search for values in sequences such as lists or tuples in Python.
Python count function syntax¶
Python’s count function call is not complicated. The function is defined on lists, strings or tuples:
type_de_séquence.count(caractère)
python
The count function is called on a sequence type using dot notation, and receives the character to search for as the first parameter. These can be numbers or individual characters, but also entire Python strings.
Python is versatile and a good choice for your web projects. The deployment of Github on the IONOS server supports you in your web projects and allows you to keep control of the current state of your project at all times.
Python count on strings¶
The count function applied to Python strings shows its usefulness if you want to count the occurrences of characters in a text. In this case, the function also takes into account additional optional parameters in the second and third positions, if necessary:
string.count(caractère, début, fin)
python
The two optional parameters expect an integer value and specify the part of the text in which you want to count occurrences of the character passed.
Count letters with Python count¶
The Python count function is easily understood using an example:
# Compter les occurrences de la lettre a
tmpstring = "les occurrences de la lettre a doivent être comptées dans cette chaîne."
tmpstring.count('a')
python
We define a string on which we can then call the count function first. The count function takes the letter « a » as a transfer parameter and therefore counts how often the letter « a » occurs in the string. As expected, this call returns the value « 4 ».
If we now want to limit the search to the letter « a » to the first ten letters of the stringthe Python count call is modified as follows:
tmpstring.count('a', 0, 10)
python
We specify the null character of the string as the starting value. As counting always starts with 0 in computing, this refers to the first letter. However, the search for the letter « a » passed ends at the tenth character of the string in our case. The reason for this is the third transfer parameter, which specifies the position from which the search should be terminated. In this case, calling the count function in Python returns the value “0”.
Count words with Python count¶
The count function is not only used to count occurrences of individual letters. It is also possible to count whole words or substrings with Python count:
# Compter les occurrences du mot « python »
tmpstring = "la méthode count en python est facile à utiliser. python est un bon langage de programmation."
tmpstring.count("python")
python
In this example, the count function is called on the tmpstring variable, and the string « python » is passed as a parameter. All occurrences of the word “python” are then counted in tmpstring. Calling the count function therefore returns the value “2”.
Python count on lists¶
In Python, many basic functions also work on lists. For example, you can use the len function to find Python List Length. The count function also takes supports use on sequence types such as Python lists. The call is made the same way as calling Python count on strings:
l = [1,2,3,1,4,5,1]
l.count(1)
python
In the sample code, a list containing various integers is first stored in the variable named l. The count function is then called with the integer value “1”. This specifies that occurrences of the number 1 should be counted in the list. Unsurprisingly, the count function returns the value « 3 » in our case, because there are three in the list.