---
description: Python count The count() is a built-in function in Python. It will return the total count of a given element in a string. The counting begins from the start of the string till the end. It is also poss
title: Python String count() with EXAMPLES
image: https://www.guru99.com/images/python-string-count.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Python string count() is a built-in method that returns how many times a character or substring appears in a string. Optional start and end arguments limit the search to a specific slice of the text.

* 🔢 **Return value:** count() returns an integer, the number of non-overlapping occurrences, or 0 when the element is absent.
* 🎯 **Start and end:** Optional start and end indexes restrict the count to a chosen portion of the string.
* 🔠 **Case sensitivity:** Counting is case-sensitive, so normalize with lower() or upper() before comparing mixed-case text.
* 📋 **Lists and tuples:** The count() method also tallies how many times an element appears inside a list or a tuple.
* 🔁 **Overlaps:** count() skips overlapping matches, so find() in a loop or a regular expression handles those cases.
* 🤖 **AI assistance:** AI coding assistants generate count() logic and count token frequencies for machine learning and NLP.

[ Read More ](javascript:void%280%29;) 

![Python String count\(\)]()

## What is Python String count()?

The **count()** method is a built-in function in Python that returns the total number of times a given element appears in a string. The counting begins from the start of the string and continues till the end. You can also specify the start and end index from where you want the search to begin.

## Syntax for Python String count()

The Python string count() function uses the following syntax:

string.count(char or substring, start, end)

### Parameters

* **char or substring:** The single character or substring you want to search for in the given string. count() returns how many times it appears in the string.
* **start:** (optional) It indicates the start index from where the search will begin. If not given, it starts from 0\. For example, when you want to search for a character from the middle of the string, you can give the start value to the count function.
* **end:** (optional) It indicates the end index where the search ends. If not given, it will search till the end of the string. For example, when you do not want to scan the entire string and prefer to limit the search to a specific point, you can give the value to end in the count function.

### Return Value

The count() method will return an integer value, that is, the count of the given element in the given string. It returns a 0 if the value is not found in the given string.

## Example 1: Count Method on a String

The following example shows the working of the count() function on a string.

str1 = "Hello World"
str_count1 = str1.count('o')  # counting the character “o” in the givenstring
print("The count of 'o' is", str_count1)

str_count2 = str1.count('o', 0,5)
print("The count of 'o' usingstart/end is", str_count2)

**Output:**

The count of 'o' is 2
The count of 'o' usingstart/end is 1

## Example 2: Count Occurrence of a Character in a Given String

The following example shows the occurrence of a character in a given string, as well as by using the start and end index.

str1 = "Welcome to Guru99 Tutorials!"
str_count1 = str1.count('u')  # counting the character “u” in the given string
print("The count of 'u' is", str_count1)

str_count2 = str1.count('u', 6,15)
print("The count of 'u' usingstart/end is", str_count2)

**Output:**

The count of 'u' is 3
The count of 'u' usingstart/end is 2

## Example 3: Count Occurrence of a Substring in a Given String

The following example shows the occurrence of a substring in a given string, as well as by using the start and end index.

str1 = "Welcome to Guru99 - Free Training Tutorials and Videos for IT Courses"
str_count1 = str1.count('to') # counting the substring “to” in the givenstring
print("The count of 'to' is", str_count1)
str_count2 = str1.count('to', 6,15)
print("The count of 'to' usingstart/end is", str_count2)

**Output:**

The count of 'to' is 2
The count of 'to' usingstart/end is 1

### RELATED ARTICLES

* [Online Python Compiler (Editor / Interpreter / IDE) to Run Code ](https://www.guru99.com/execute-python-online.html "Online Python Compiler (Editor / Interpreter / IDE) to Run Code")
* [Facebook Login using Python: FB Login Example ](https://www.guru99.com/facebook-login-using-python.html "Facebook Login using Python: FB Login Example")
* [Python enumerate() Function: Loop, Tuple & String ](https://www.guru99.com/python-enumerate-function.html "Python enumerate() Function: Loop, Tuple & String")
* [\[::-1\] in Python with Examples ](https://www.guru99.com/1-in-python.html "[::-1] in Python with Examples")

## Python count() Method on a List

The count() method is not limited to strings. Python lists and tuples also provide a count() method that returns how many times a specific element appears in the sequence. Unlike the string version, list.count() and tuple.count() accept only the element to search for, and they do not take start or end index arguments.

fruits = ['apple', 'banana', 'apple', 'grape', 'apple']
print(fruits.count('apple'))  # counts how many times 'apple' appears

numbers = (1, 2, 2, 3, 2)
print(numbers.count(2))  # count() also works on a tuple

**Output:**

3
3

Because count() belongs to both strings and sequences, the same method name works whether you are counting characters in text or repeated items in a collection.

## How to Count Overlapping Occurrences in Python

The count() method only counts non-overlapping occurrences of a substring. When matches can overlap, count() reports fewer results than you might expect. For example, counting “aa” inside “aaaa” returns 2 rather than 3, because count() moves past each match it finds.

text = "aaaa"
print(text.count("aa"))          # non-overlapping count is 2

import re
overlaps = len(re.findall("(?=(aa))", text))
print(overlaps)                  # overlapping count is 3

**Output:**

2
3

To count overlapping matches, use the re module with a lookahead assertion, or loop with the find() method, advancing the search position by one character after every match.

» Learn more about [Python String methods](https://www.guru99.com/learning-python-strings-replace-join-split-reverse.html)

## FAQs

🔠 Is the Python string count() method case-sensitive?

Yes. count() matches characters exactly, so “Hello”.count(“h”) returns 0 while “Hello”.count(“H”) returns 1\. Convert the string with lower() or upper() first when you need case-insensitive counting.

🔢 What does Python count() return when the element is not found?

count() returns 0 when the character or substring does not appear in the string. It never raises an error for a missing element, which makes it safe to use directly inside conditions and comparisons.

␣ What happens if you pass an empty string to count()?

Passing an empty string returns the length of the string plus one, because Python counts the empty matches between every character and at both ends. For “abc” the result is 4.

🎯 Can count() search for several different characters at once?

No. count() accepts one character or substring per call. To tally many characters together, use collections.Counter, which returns the frequency of every character in the string in a single pass.

⚖️ What is the difference between count() and find() in Python?

count() returns how many times a substring appears, while find() returns the index of its first occurrence, or -1 if it is absent. Use count() for totals and find() for locating a position.

⚡ Is str.count() faster than writing a manual loop?

Yes. count() is implemented in C, so it scans the string much faster than an equivalent Python for-loop. Prefer count() for both readability and speed when you only need non-overlapping totals.

🤖 How can AI help count and analyze text patterns for machine learning?

AI assistants generate count() and collections.Counter code from plain-language prompts. In machine learning and NLP, counting word or token frequencies builds features such as bag-of-words vectors that models use for classification.

🧠 Can GitHub Copilot generate Python count() code automatically?

Yes. GitHub Copilot autocompletes count() calls from a short comment describing your goal. Agentic AI tools go further, writing the counting logic, adding start and end arguments, and running your tests for you.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/python-string-count.png","url":"https://www.guru99.com/images/python-string-count.png","width":"700","height":"250","caption":"Python String count()","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/python-string-count.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/python","name":"Python"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/python-string-count.html","name":"Python String count() with EXAMPLES"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/python-string-count.html#webpage","url":"https://www.guru99.com/python-string-count.html","name":"Python String count() with EXAMPLES","dateModified":"2026-07-10T17:17:19+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/python-string-count.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/python-string-count.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/logan","name":"Logan Young","description":"I'm Logan Young, an expert in Python, providing top-tier tutorials and guides to enhance your coding skills and streamline your learning journey.","url":"https://www.guru99.com/author/logan","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/logan-young-author.png","url":"https://www.guru99.com/images/logan-young-author.png","caption":"Logan Young","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Python","headline":"Python String count() with EXAMPLES","description":"Python count The count() is a built-in function in Python. It will return the total count of a given element in a string. The counting begins from the start of the string till the end. It is also poss","keywords":"python","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/logan","name":"Logan Young"},"dateModified":"2026-07-10T17:17:19+05:30","image":{"@id":"https://www.guru99.com/images/python-string-count.png"},"copyrightYear":"2026","name":"Python String count() with EXAMPLES","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Is the Python string count() method case-sensitive?","acceptedAnswer":{"@type":"Answer","text":"Yes. count() matches characters exactly, so \"Hello\".count(\"h\") returns 0 while \"Hello\".count(\"H\") returns 1. Convert the string with lower() or upper() first when you need case-insensitive counting."}},{"@type":"Question","name":"What does Python count() return when the element is not found?","acceptedAnswer":{"@type":"Answer","text":"count() returns 0 when the character or substring does not appear in the string. It never raises an error for a missing element, which makes it safe to use directly inside conditions and comparisons."}},{"@type":"Question","name":"What happens if you pass an empty string to count()?","acceptedAnswer":{"@type":"Answer","text":"Passing an empty string returns the length of the string plus one, because Python counts the empty matches between every character and at both ends. For \"abc\" the result is 4."}},{"@type":"Question","name":"Can count() search for several different characters at once?","acceptedAnswer":{"@type":"Answer","text":"No. count() accepts one character or substring per call. To tally many characters together, use collections.Counter, which returns the frequency of every character in the string in a single pass."}},{"@type":"Question","name":"What is the difference between count() and find() in Python?","acceptedAnswer":{"@type":"Answer","text":"count() returns how many times a substring appears, while find() returns the index of its first occurrence, or -1 if it is absent. Use count() for totals and find() for locating a position."}},{"@type":"Question","name":"Is str.count() faster than writing a manual loop?","acceptedAnswer":{"@type":"Answer","text":"Yes. count() is implemented in C, so it scans the string much faster than an equivalent Python for-loop. Prefer count() for both readability and speed when you only need non-overlapping totals."}},{"@type":"Question","name":"How can AI help count and analyze text patterns for machine learning?","acceptedAnswer":{"@type":"Answer","text":"AI assistants generate count() and collections.Counter code from plain-language prompts. In machine learning and NLP, counting word or token frequencies builds features such as bag-of-words vectors that models use for classification."}},{"@type":"Question","name":"Can GitHub Copilot generate Python count() code automatically?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot autocompletes count() calls from a short comment describing your goal. Agentic AI tools go further, writing the counting logic, adding start and end arguments, and running your tests for you."}}]}],"@id":"https://www.guru99.com/python-string-count.html#schema-1140208","isPartOf":{"@id":"https://www.guru99.com/python-string-count.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/python-string-count.html#webpage"}}]}
```
