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 8971371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:56:17+00:00 2026-06-15T17:56:17+00:00

Possible Duplicate: String comparison in Python: is vs. == algorithm = str(sys.argv[1]) print(algorithm) print(algorithm

  • 0

Possible Duplicate:
String comparison in Python: is vs. ==

algorithm = str(sys.argv[1])
print(algorithm)
print(algorithm is "first")

I’m running it from the command line with the argument first, so why does that code output:

first
False
  • 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-15T17:56:18+00:00Added an answer on June 15, 2026 at 5:56 pm

    From the Python documentation:

    The operators is and is not test for object identity: x is y is true if and only if x and y are the same object.

    This means it doesn’t check if the values are the same, but rather checks if they are in the same memory location. For example:

    >>> s1 = 'hello everybody'
    >>> s2 = 'hello everybody'
    >>> s3 = s1
    

    Note the different memory locations:

    >>> id(s1)
    174699248
    >>> id(s2)
    174699408
    

    But since s3 is equal to s1, the memory locations are the same:

    >>> id(s3)
    174699248
    

    When you use the is statement:

    >>> s1 is s2
    False
    >>> s3 is s1
    True
    >>> s3 is s2
    False
    

    But if you use the equality operator:

    >>> s1 == s2
    True
    >>> s2 == s3
    True
    >>> s3 == s1
    True
    

    Edit: just to be confusing, there is an optimisation (in CPython anyway, I’m not sure if it exists in other implementations) which allows short strings to be compared with is:

    >>> s4 = 'hello'
    >>> s5 = 'hello'
    >>> id(s4)
    173899104
    >>> id(s5)
    173899104
    >>> s4 is s5
    True
    

    Obviously, this is not something you want to rely on. Use the appropriate statement for the job – is if you want to compare identities, and == if you want to compare values.

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

Sidebar

Related Questions

Possible Duplicate: How do I do a case insensitive string comparison in Python? I
Possible Duplicate: remove duplicate from string in PHP Have a string like: $str =
Possible Duplicate: String comparison in Python: is vs. == When is the == operator
Possible Duplicate: How does Python compare string and int? I was doing some comparison
Possible Duplicate: String comparison in Python: is vs. == Python string interning Why does
Possible Duplicate: String comparison in Objective-C I realize that the question is not very
Possible Duplicate: Case insensitive string comparison in C++ C++ count and map How to
Possible Duplicate: How do I compare strings in Java? Demonstrating string comparison with Java
Possible Duplicate: How do I compare strings in Java? why first comparison ( s1
Possible Duplicate: String comparison in dotnet framework 4 I noticed a performance problem on

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.