logo

Python String Count() Method


Show

Description

The count() method in Python String returns the number of occurrences of substring sub in the range [start, end]. Optional arguments start and end are interpreted as in slice notation.

Syntax

str.count(sub, start= 0,end=len(string))

Parameters

  • sub - It is the substring that is to be searched.
  • start - Search begins from this index. The first character begins from 0 indexes. By default search begins from 0 indexes.
  • end - Search comes to end from this index. The first character begins from 0 indexes. By default search comes to an end at the last index.

Return Value

Centered in a string of length width.

Example

#!/usr/bin/python

str = "this is string example....wow!!!";

sub = "i";
print "str.count(sub, 4, 40) : ", str.count(sub, 4, 40)
sub = "wow";
print "str.count(sub) : ", str.count(sub)

Result

str.count(sub, 4, 40) :  2
str.count(sub) :  1

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.