This article mainly introduces the use of the count () method for Python string processing. It is the basic knowledge for getting started with Python. For more information, see count () returns the number of substrings in the range [start, end]. The start and end parameters of the optional parameters are interpreted as slice symbols.
Syntax
The syntax of the count () method is as follows:
Str. count (sub, start = 0, end = len (string ))
Parameters
- Sub -- this is a substring used for search.
- Start -- search from this index. The index with the first character starting from 0. By default, indexes starting from 0 are searched.
- End -- the search end from the index. The index with the first character starting from 0. By default, the last index after the search ends.
Return Value
This method returns a string that is concentrated in the length and width.
Example
The following example shows how to use the count () method.
#! /Usr/bin/pythonstr = "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)
When we run the above program, it will produce the following results:
Str. count (sub, 4, 40): 2str. count (sub, 4, 40): 1