Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8276069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:16:05+00:00 2026-06-08T08:16:05+00:00

I was trying to understand the functioning of string.count basically The documented definition of

  • 0

I was trying to understand the functioning of string.count basically
The documented definition of it is

string.count(s, sub[, start[, end]])
Return the number of (non-overlapping) occurrences of substring sub in string s[start:end]. Defaults for start and end and
interpretation of negative values are the same as for slices.

Now what’s the exact meaning of last line here which is bold actually. I did some random experiment with this function

In [19]: a
Out[19]: 'ab'
In [23]: string.count(a,'ab'[1:3])
Out[23]: 1

In [24]: string.count(a,'ab'[1:1])
Out[24]: 3

In [25]: string.count(a,'ab'[-1:1])
Out[25]: 3

In [26]: string.count(a,'ab'[-1:0])
Out[26]: 3

In [27]: string.count(a,'ab'[1:4])
Out[27]: 1

In [28]: string.count(a,'ab'[1:100])
Out[28]: 1

In [29]: string.count(a,'ab'[100:100])
Out[29]: 3

In [30]: string.count(a,'ab'[:])
Out[30]: 1

In [31]: string.count(a,'a'[:])
Out[31]: 1

Can anybody explain me why some times I am getting result as 1 and why sometimes 3 here. Overall I need to understand how this function works exactly ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T08:16:07+00:00Added an answer on June 8, 2026 at 8:16 am

    Everytime you see a 3 as result, the second parameter was the empty string – you hide this facte by using strange slicings. The empty string is considered to occur at the positions 0:0, 1:1 and 2:2 for this example, so there are three occurrences. In general, string.count(s, "") (or equivalently, s.count("")) will return len(s) + 1.

    To explain how this result comes about, here is a (not very efficient) example implementation of string.count():

    def count(s, sub):
        result = 0
        for i in range(len(s) + 1 - len(sub)):
            result += (s[i:i + len(sub)] == sub)
        return result
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand how the code below always brings up a number between 0-6
I have the following program in which I'm trying to understand the functioning of
I am trying understand ViewModels deeper and I have read many articles and blogs
Trying to understand what's the correct way of implementing OpenID authentication with Spring Security.
Trying to understand the Deezer API. When I visit: http://connect.deezer.com/oauth/auth.php?app_id=MY_APP_ID&redirect_uri=http://mydomain.me&perms=basic_access I end up at
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand something. I created a d:\svn\repository on my server. I committed folders
Trying to understand the math of this code snippet. A token is provided which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.