Use of the count () method for Python string processing

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.