Python3 count () method
description
count () method is used to count the number of times a character appears in a string. Optional parameters at the start and end of the string to search for.
grammar
count () method syntax:
str.count(sub, start= 0,end=len(string))
parameter
- Substring search - sub
- start - beginning of the string search. The default is the first character, the first character index value of zero.
- end - the end of the string search. Character index of the first character is 0. The default is the last position of the string.
return value
This method returns the number of substring in the string.
Examples
The following example shows an example of count () method:
#!/usr/bin/python3 str="www.w3big.com" sub='o' print ("str.count('o') : ", str.count(sub)) sub='run' print ("str.count('run', 0, 10) : ", str.count(sub,0,10))
Examples of the above output results are as follows:
str.count('o') : 3 str.count('run', 0, 10) : 1