count(3,2) will start from 3 and increase by interval 2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

count(3,2) will start from 3 and increase by interval 2

3,5,7,9.....

28th Oct 2016, 12:18 AM
Shekhar Bahuguna
Shekhar Bahuguna - avatar
4 Answers
+ 2
Use a generator. Edit: Just saw 'itertools' tag. Here's the doc section: https://docs.python.org/3/library/itertools.html#itertools.count
28th Oct 2016, 5:27 AM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Count is a string function used to find occurrence of any substring. Here it's basic synatax count(sub, start= 0,end=len(string)) count ("i",4,30) First argument is for sub string ..... second is starting point and third is ending point. see below example and hope it will help you str = "this is simple string example ... " sub = "i" print ("str.count(sub) : ", str.count(sub)) #Above code will search for whole string to count 'a' in string are #Output will be 4 print ("str.count(sub, 0, 30) : ", str.count(sub, 0, 30)) #Output will be 4 print ("str.count(sub, 4, 30) : ", str.count(sub, 4, 30)) #Output will be 3 sub = "w" print ("str.count(sub) : ", str.count(sub)) #Output will be 0
28th Oct 2016, 5:43 AM
Waqas Asghar Bhalli
Waqas Asghar Bhalli - avatar
+ 1
The currently upvoted answer isn't related to itertools so the vote load seems peculiar. Hopefully that doesn't burn you if you're answering a test question. Note that itertools uses a generator as an inside example, but that example's still slower than count(). Best of luck.
28th Oct 2016, 8:02 PM
Kirk Schafer
Kirk Schafer - avatar
0
here's what I think is a better explanation of count(): say you have a str or a list, and want to know how many of an "item" are in that str/list, list.count("item",a,b) where list is the name of the list, "item" is the item you're looking for, a is where to start looking from, and b is where to stop. Note that you don't necessarily need an a and b, you can write it like this : list.count("item")
9th Jan 2017, 2:45 AM
ramzi
ramzi - avatar