Python String count() Method

By Lenin Mishra

If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.

The count() method returns the number of times a substring s1 appears in the string s2. It is not case sensitive.

Syntax of count() method

string.count(value, start, end)

value:	The substring to search for(Required)
start:	The integer position to start the search. Default is 0.(Optional)
end:	The integer position to end the search. Default is the end of the string.(Optional)
Example 1

Code

s2 = "I like Pylenin"
s1 = 'like'

if s2.count(s1) > 0:
    print(f"'{s1}' exists in '{s2}'")

Output

'like' exists in 'I like Pylenin'
Example 2 - Check for case sensitivity

Code

s2 = "I like Pylenin"
s1 = 'Like'

if s2.count(s1) > 0:
    print(f"'{s1}' exists in '{s2}'")
else:
    print(f"'{s1}' does not exist in '{s2}'")

Output

'Like' does not exist in 'I like Pylenin'
Example 3 - Search for a non-existing string.

Code

s2 = "I like Pylenin"
s1 = 'Python'

if s2.count(s1) > 0:
    print(f"'{s1}' exists in '{s2}'")
else:
    print(f"'{s1}' doesn't exist in '{s2}'")

Output

'Python' doesn't exist in 'I like Pylenin'
Example 4 - Search a substring s1 in a specific portion of string s2.

Code

s2 = "I like reading Pylenin blogs on Python"
s1 = 'Python'

# Search between position 10 and 20
if s2.count(s1, 10, 20) > 0:
    print(f"'{s1}' exists in '{s2}'")
else:
    print(f"'{s1}' doesn't exist in '{s2}'")

print(s2.find(s1))

Output

'Python' doesn't exist in 'I like reading Pylenin blogs on Python'
32

As you can see, the substring Python doesn’t occur between the positions 10 and 20. The find() method shows that it occurs at position 32.

Check out other commonly used Python string methods.

  1. How to create a string in Python?
  2. How to access characters in a Python string?
  3. How to replace characters in a string in Python?
  4. How to concatenate strings in Python?
  5. How to iterate through a string in Python?
  6. Check if a Substring is Present in a Given String in Python
  7. Escape sequences in Python String
  8. Python String Formatting - The Definitive Guide