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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:46:42+00:00 2026-05-24T06:46:42+00:00

Can someone explain this to me? So I’ve been playing with the id() command

  • 0

Can someone explain this to me? So I’ve been playing with the id() command in python and came across this:

>>> id('cat')
5181152
>>> a = 'cat'
>>> b = 'cat'
>>> id(a)
5181152
>>> id(b)
5181152

This makes some sense to me except for one part: The string ‘cat’ has an address in memory before I assign it to a variable. I probably just don’t understand how memory addressing works but can someone explain this to me or at least tell me that I should read up on memory addressing?

So that is all well and good but this confused me further:

>>> a = a[0:2]+'t'
>>> a
'cat'
>>> id(a)
39964224
>>> id('cat')
5181152

This struck me as weird because ‘cat’ is a string with an address of 5181152 but the new a has a different address. So if there are two ‘cat’ strings in memory why aren’t two addresses printed for id(‘cat’)? My last thought was that the concatenation had something to do with the change in address so I tried this:

>>> id(b[0:2]+'t')
39921024
>>> b = b[0:2]+'t'
>>> b
'cat'
>>> id(b)
40000896

I would have predicted the IDs to be the same but that was not the case. Thoughts?

  • 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-24T06:46:43+00:00Added an answer on May 24, 2026 at 6:46 am

    Python reuses string literals fairly aggressively. The rules by which it does so are implementation-dependent, but CPython uses two that I’m aware of:

    • Strings that contain only characters valid in Python identifiers are interned, which means they are stored in a big table and reused wherever they occur. So, no matter where you use "cat", it always refers to the same string object.
    • String literals in the same code block are reused regardless of their content and length. If you put a string literal of the entire Gettysburg Address in a function, twice, it’s the same string object both times. In separate functions, they are different objects:
      def foo(): return "pack my box with five dozen liquor jugs"
      def bar(): return "pack my box with five dozen liquor jugs"
      assert foo() is bar() # AssertionError

    Both optimizations are done at compile time (that is, when the bytecode is generated).

    On the other hand, something like chr(99) + chr(97) + chr(116) is a string expression that evaluates to the string "cat". In a dynamic language like Python, its value can’t be known at compile time (chr() is a built-in function, but you might have reassigned it) so it normally isn’t interned. Thus its id() is different from that of "cat". However, you can force a string to be interned using the intern() function. Thus:

    id(intern(chr(99) + chr(97) + chr(116))) == id("cat")   # True
    

    As others have mentioned, interning is possible because strings are immutable. It isn’t possible to change "cat" to "dog", in other words. You have to generate a new string object, which means that there’s no danger that other names pointing to the same string will be affected.

    Just as an aside, Python also converts expressions containing only constants (like "c" + "a" + "t") to constants at compile time, as the below disassembly shows. These will be optimized to point to identical string objects per the rules above.

    >>> def foo(): "c" + "a" + "t"
    ...
    >>> from dis import dis; dis(foo)
      1           0 LOAD_CONST               5 ('cat')
                  3 POP_TOP
                  4 LOAD_CONST               0 (None)
                  7 RETURN_VALUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone explain to me this odd thing: When in python shell I type
Can someone explain this weird behavior of python re? Obviously the string 'test' does
Can someone explain some behaviour I'm seeing in SQL Server 2005? I've been tasked
Can someone explain this result to me. The first test succeeds but the second
Can someone explain this to me. I am pretty good with perl regular expressions
Can someone explain this to me? In particular the difference between: http://github.com/whymirror/greg and http://piumarta.com/software/peg/
Can someone explain what this means? int (*data[2])[2];
Can someone explain why this doesn't work? int main() { float* x; float a,
Can someone explain why this script throws an exception? $byteArray = @(1,2,3) write-Output (
I am newbie to jQuery, can someone explain what this code does: $(#currency form).submit(function(e)

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.