Python String count() Method


Contents

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 which is to be searched.
  • start − Search begins from this index. First character begins from 0 index. By default search begins from 0 index.
  • end − Search comes to end from this index. First character begins from 0 index. 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.



Navigation
Tutorial
Tutorial
Tutorial