Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

string
python
count
communitycreator

What is the string.count method in Python?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The count method returns the number of occurrences of the substring of a string.

Syntax

string.count(substring, startIndex, endIndex)

Parameters

  • substring: The substring to be searched on the string.

  • startIndex: The index from which the substring is to be searched. It is an optional value, and by default the value is 0.

  • endIndex: The index to which the substring is to be searched. It is an optional value, and by default the value is string.length.

Return value

The return value is the number of non-overlapping occurrences of the substring in a string.

Example

string = "educative";
print("Number of 'e' in the string ", string)
print(string.count('e'))
string = "iii"
print("\nNumber of 'ii' in the string ", string)
print(string.count('ii'))
string = "educative"
print("\nNumber of 'e' in the string ", string, "from index 1 is")
print( string.count('e', 1) ) # this will search for 'e' from index 1 to end of the string
string = "educative"
print("\nNumber of 'e' in the string ", string, "between index 1 and 3 is")
print( string.count('e', 1, 3) ) # this will search for 'e' from index 1 to 3rd index of the strinf

In the above code, we:

  • Created an educative string and used the count method to check how many e are present in the string. The count method will return 2.

  • Created an iii string and used the count method to check how many ii are present in the string. The count method will return 1 because in the iii string, the first two ii characters match the substring, so the next search will happen from index 2. The search of the substring will take place in a non-overlapping manner.

  • Created a string educative and checked how many e are present in the string from the index 1 using the count method. The count method will return 1.

  • Created an educative string and used the count method to check how many e are present in the string between the index 1 and 3. The count method will return 0 because no e is present between the index 1 - 3.

RELATED TAGS

string
python
count
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring