I am trying to count the output in python. 'len' seems to be not working with the output. Following is my code
for i in range (0, 5): print iThe output is
0 1 2 3 4I want to print the count of 'i' which should give me count of '5'
Thank you in advance!
The6thSense
7,7636 gold badges30 silver badges63 bronze badges
asked Aug 20, 2015 at 8:45
0
Use enumerate to count the iterations:
count = 0 for count, i in enumerate(range(5),1): print(i) print(count)If you want the size of a list use len:
print(len(range(5)))answered Aug 20, 2015 at 8:48
1
There are many ways to do but my two ways would be
One way is to use len
i =len(range (0, 5)) print iOther way is using for:
j=0 for i in range (0, 5): #Other things you want to do j+=1 print janswered Aug 20, 2015 at 8:49
The6thSenseThe6thSense
7,7636 gold badges30 silver badges63 bronze badges
2
Learn about count() function in Python.
The count() function of the python is used to count the frequency of the character or a substring in a string. It simply gives the count of occurrences as a return value. Python Count() function has following syntax: string.count(substring/character, start=, end=)
Three parameters are accepted by the count() function of python while using strings:Overview
Syntax of count() Function in Python
Parameters of count() Fucntion in Python
- substring/character: Which is to be searched in the string.
- start (optional): The value which is stated as the start position. It is inclusive, which means substring/character at this position is also included in the count. If we leave this parameter empty, then start is considered as 0th position by default.
- end (optional): The value stated as the end position. It is exclusive, which means substring/character at this position is excluded from the count. If we leave this parameter empty, then the end is considered the last index of the string by default.
Return Value of count() Fucntion in Python
Return Type: integer
It returns the number of occurrences of substring/character in a string.
Example of count() Fucntion in Python
Let's use count() function on strings to find the occurrence of any character.
string1 = 'Hello There' occurrenceOfE = string1.count('e') print('occurrence of e in string: ', occurenceOfE)
Output
occurrence of e in string: 3
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of the character 'e' in the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of 'e' in the string.
What is the count() Function in Python?
Let's say you have a very long binary string(a string that only consists of 0's and 1's), and you have a tedious job of counting the number of occurrences of '1' in that string. Have you ever wondered how you would solve this long task within a single second? If you use Python Count() function to find the number of occurrences of '1' in that string, you get desired output within milliseconds.
The python count() function is an inbuilt function of python which is used to count the number of occurrences of a character or substring in the string. Count function is case-sensitive it means that 'a' & 'A' are not treated as the same.
Application of count() Function in Python
- Count function can find the white spaces in a given string.
- Count function can determine the frequency of any character in a given string.
- Count function can also be used to find out the count of a given word from a string.
Python count() function is used with both string and array/list. When used with array and list, the count() function have a different syntax and parameter but the same return value.
More Examples
Example 1: Using Count Method with a Substring.
Let's use count() function on strings to find the occurrence of any of its substring(more than one character) in that string.
string1 = 'abcdabcdabcdbcdefghqwert' occurrenceOfSubstring = string1.count('abcd') print("Occurrence of 'abcd' in string1: ", occurrenceOfSubstring)
Output
Occurrence of 'abcd' in string1: 3
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring 'abcd' from the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of 'abcd' in the string as an output.
Example 2: Count method with Character in a Given Binary String
Let's use count() on strings to find the occurrence of any of its character in that binary string.
string1 = '010001111101010111101110111110000011111101010101' occurrenceOf0 = string1.count('0') print('Occurrence of 0 in string1: ', occurrenceOf0)
Output
Occurrence of 0 in string1: 18
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of character '0' from the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of '0' in the string as an output.
Example 3: Count method with Substring in a Given Binary String
Let's use count() on strings to find the occurrence of any of its substring(more than one character) in that string.
string1 = '010001111101010111101110111110000011111101010101' occurrenceOf01 = string1.count('01') print('Occurrence of "01" in string1: ', occurrenceOf01)
Output
Occurrence of '01' in string1: 12
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring '01' from string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of '01' in the string as an output.
Example 4: Using Start Position Parameters
Let's use count() on strings to find the occurrence of any of its substring(more than one character) in that string by passing only one optional parameter start, for specifying the start position for finding the substring.
string1 = 'ab bc ba ab bc cb xz qs ab hj ab ja ab' occurenceOfSubString = string1.count('ab', 11) print('Occurence of substring in string1: ', occurenceOfSubString)
Output
Occurrence of a substring in string1: 3
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring 'ab' in a string starting from position 11(inclusive) till the end of the string and stored it in the variable.
- Finally, we printed that variable and got the occurrence of 'ab' in the string as an output.
Example 5: Using End Position Parameters
Let's use count() function on strings to find the occurrence of any of its substring(more than one character) in that string by passing only one optional parameter end for specifying the end position till there we have to find the substring.
While we are only sending a single optional parameter end in count() function. We have to use start parameter as None to specify that we have to start counting from the start of the string.
string1 = 'ab bc ba ab bc cb xz qs ab hj ab ja ab' occurenceOfSubString = string1.count('ab', None, 15) print('Occurence of substring in string1: ', occurenceOfSubString)
Output
Occurrence of a substring in string1: 2
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of substring 'ab' in the string from starting of the string till position 15(exclusive) and stored it in the variable.
- Finally, we printed that variable and got the occurrence of 'ab' in the string as an output.
Example 6: Using both Optional Parameters
Let's use count() on strings to find the occurrence of any of its characters in that string by passing some additional optional parameters start & end as discussed earlier.
string1 = 'Computer Science' occurrenceOfE = string1.count('e', 3, 14) print('Occurrence of e from 3rd to 14th index in string1: ', occurrenceOfE)
Output
Occurrence of e from 3rd to 14th index in string1: 2
Explanation
- We created a string and stored it into a variable.
- Then, we calculated the occurrence of character 'e' in the string from the position between 3(inclusive) and 14(exclusive) and then stored it in the variable.
- Finally, we printed that variable and got the occurrence of 'e' in the string as an output.
Conclusion
First of all, we understood what count() in python and the basic use of the count function, and we discussed the syntax, parameters, and the return value of the count function.
The function returns the number of occurrences of provided character/substring in the string.
We can provide the start and end position of the string to count the occurrences in only a specified portion of the string.