python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

python

Can someone please explain me the below code str1 = "my isname isisis jameis isis bond"; sub = "is"; print(str1.count(sub,4)) # Output:- 6 #----------------------------------------------------------------- str1 = "my isname isisis jameis isis bond"; sub = "is"; print(str1.count(sub,2)) # Output:- 7

24th Aug 2022, 6:21 AM
Amol Bhandekar
Amol Bhandekar - avatar
3 Answers
+ 2
Amol Bhandekar count(string, start_position); #start_position - index In first case start position is 4 so it will be count after the 5th character In second case start position is 2 so it will be count after the 3rd character
24th Aug 2022, 6:33 AM
A͢J
A͢J - avatar
+ 3
Quoted from https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_string_count.asp "The count() method returns the number of times a specified value appears in the string. Syntax: string.count(value, start, end)" The method allows optional arguments - a range of indices which specifies from which index and to which index should the argument be counted. If <start> index is omitted, the counting starts from 0th character. If <end> index is omitted, the counting runs to the end of the string - minus length of <value>
24th Aug 2022, 6:43 AM
Ipang
+ 1
Thank AJ Understood means here we used indexing...
24th Aug 2022, 6:40 AM
Amol Bhandekar
Amol Bhandekar - avatar