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

  • Home
  • SEARCH
  • 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 6339453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:39:07+00:00 2026-05-24T19:39:07+00:00

I have always thought that using -1 in a condition is alway the same

  • 0

I have always thought that using -1 in a condition is alway the same as the writing False (boolean value). But from my code, I get different results:

Using True and False:

def count(sub, s):
    count = 0
    index = 0
    while True:
        if string.find(s, sub, index) != False:
            count += 1
            index = string.find(s, sub, index) + 1
        else:
            return count


print count('nana', 'banana')

Result: Takes to long for interpreter to respond.


Using 1 and -1:

def count(sub, s):
    count = 0
    index = 0
    while 1:
        if string.find(s, sub, index) != -1:
            count += 1
            index = string.find(s, sub, index) + 1
        else:
            return count


print count('nana', 'banana')

Result: 1

Why does using -1 and 1 give me the correct result whereas using the bool values True and False do not?

  • 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-05-24T19:39:07+00:00Added an answer on May 24, 2026 at 7:39 pm

    string.find doesn’t return a boolean so string.find('banana', 'nana', index) will NEVER return 0 (False) regardless of the value of index.

    >>> import string
    >>> help(string.find)
    Help on function find in module string:
    
    find(s, *args)
        find(s, sub [, start [, end]]) -> int
    
        Return the lowest index in s where substring sub is found,
        such that sub is contained within s[start,end].  Optional
        arguments start and end are interpreted as in slice notation.
    
        Return -1 on failure.
    >>>
    

    Your example simply repeats:

    index = string.find('banana', 'nana', 0) + 1 # index = 3
    index = string.find('banana', 'nana', 3) + 1 # index = 0
    

    The -1 version works because it correctly interprets the return value of string.find!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always thought that in order to connect to SQL server using windows
I have always thought that the .equals() method in java should be overridden to
I have always thought that the terms internationalization and localization (and their funny abbreviations
I have always made a point of writing nice code comments for classes and
I have always been for documenting code, but when it comes to AJAX +
I am a PHP developer and I have always thought that micro-optimizations are not
This is something that I have always wondered about, but never bothered to profile.
I have always thought that replacing the normal windows desktop icons with an active
So I always thought that seeing Using Where in the Extra column was a
I have always thought it was best practice to be explicit in naming my

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.