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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:29:50+00:00 2026-06-16T02:29:50+00:00

Python strings are supposed to be immmutable just like integers. Consider this: >>> num1

  • 0

Python strings are supposed to be immmutable just like integers. Consider this:

>>> num1 = 34
>>> num2 = 36
>>> id(num1)
505894832
>>> num4 = 34
>>> id(num4)
505894832

num4 has the exact same ID as num1, which means they are pointing to the same thing. Shouldn’t the same thing happen with strings ? Am confused with:

>>> name = "Sumeet"
>>> id(name)
35692000
>>> name = "Ali"
>>> id(name)
35926912
>>> naam = "Sumeet"
>>> id(naam)
35926848

Shouldn’t the last output be: 35692000?

  • 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-16T02:29:52+00:00Added an answer on June 16, 2026 at 2:29 am

    The fact that several variables have the same id has nothing much to do with the actual objects being immutable.

    In fact, this can happen safely (saving memory), due to their immutability.

    Let’s assume that a string in python was not immutable, you declared:

    a = 'abc'

    b = 'abc'

    If you changed a, that would mean that b would either reference a completely different object (duplicating the memory needed to represent the same literal string), or that, when a was changed, the whole object would have to be copied over in order to make the change (so that b wouldn’t be affected).

    Since the strings are immutable, both variables can safely point to the same object. Any change to an immutable data structure creates a new structure, and the reference that was pointing towards it is changed to the new one, leaving all other references to the “old” structure unchanged. The absence of side effects in immutable data structures greatly diminishes the possibility of errors occurring due to a shared structure/object being changed somewhere else in your code.

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

Sidebar

Related Questions

I have about 50 million lists of strings in Python like this one: [1,
Languages like Python, MATLAB, E-Lisp have this nice facility for doc-strings. With this feature
How to document methods with parameters using Python's documentation strings? PEP 257 gives this
I would like to perform a bitwise exclusive or of two strings in python,
What is the proper indentation for Python multiline strings within a function? def method():
I'm having trouble using python to write strings into a file: (what I'm trying
Is there a quick way in Python to replace strings but, instead of starting
My doc strings have references to other python classes that I've defined. Every time
Does Python have a pool of all strings and are they (strings) singletons there?
Possible Duplicate: Python ‘==’ vs ‘is’ comparing strings, ‘is’ fails sometimes, why? In Python,

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.