Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python3 count() Method

Python3 count() Method

Python3 Strings Python3 Strings


Description

The count() method counts how many times a character occurs in a string. Optional parameters specify the start and end positions of the search within the string.

Syntax

count() method syntax:


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

Parameters

Return Value

This method returns the number of times the substring occurs in the string.

Example

The following example demonstrates the count() method:

Example

#!/usr/bin/python3

str

=

"www.runoob.com"

sub

=

'o'

print

(

"str.count('o') : "

,

str

.

count

(

sub

)

)

sub

=

'run'

print

(

"str.count('run', 0, 10) : "

,

str

.

count

(

sub

,

0

,

10

)

)

The output of the above example is:


str.count('o') :  3
str.count('run', 0, 10) :  1

Python3 Strings Python3 Strings