Easy Tutorial
❮ Ref Math Remainder Python Func Hex ❯

Python3 count() Method

Python3 Strings


Description

The count() method is used to count the number of occurrences of a character in a string. Optional parameters specify the start and end positions for the search within the string.

Syntax

The syntax for the count() method is:

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

Parameters

Return Value

This method returns the number of occurrences of the substring in the string.

Example

The following example demonstrates the use of the count() method:

#!/usr/bin/python3

str = "www.tutorialpro.org"
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

❮ Ref Math Remainder Python Func Hex ❯