Describe
The Python count () method is used to count the occurrences of a character in a string. The optional parameters are the start and end positions of the string search.
Grammar
Count () method syntax:
Str. Count(Sub, start=0,end=len(string))
Parameters
- Sub--The substring of the search
- Start--the location where the string starts searching. The default is the first character, and the first character index value is 0.
- End--the location of the ending search in the string. The index of the first character in a character is 0. The default is the last position of the string.
return value
The method returns the number of occurrences of a substring in a string.
Instance
The following example shows an instance of the Count () method:
me ="I ' m change, it's a vegetable bag."Print("me.count (' E '):", Me.count ("e"))Print("me.count (' e ', 2,7):", Me.count ("e", 2,7))Print("me.count (' e ', 7,-1):", Me.count ("a", 7,-1))
The result of the above example output is as follows:
Me.count ('e'): 4me.count ('e', 2,7): 0me.count ('e', 7,-1): 3
STR string capitalize () method