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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:39:43+00:00 2026-06-13T21:39:43+00:00

I have a look at python code where string variable assignments looks like: var1

  • 0

I have a look at python code where string variable assignments looks like:

var1 = var2[:]

I am just wondering what is the difference between:

var1 = var2

Here is my experiments:

>>> original = "some text"
>>> copy1 = original
>>> copy2 = original[:]
>>> original = "another text"
>>> copy1
'some text'
>>> copy2
'some text'

Updated:

Here is a full code. This code search a key for substitution cipher.
If I remove ‘[:]’ this code will works very slowly.

  • 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-13T21:39:45+00:00Added an answer on June 13, 2026 at 9:39 pm

    Due to interning, there is often no difference (in the resulting object) between the two. We may check whether two variables point to the same object by using the is operator, which in contrast to the == operator checks wheter the actual memory address of the objects are the same:

    >>> a = "foo"
    >>> b = a
    >>> a is b
    True
    >>> c = a[:]
    >>> a is c
    True
    

    Interning is a mechanism for saving memory and speeding up comparisons of immutable objects, and it works like this: before creating a new immutable, python checks to see if an identical immutable already exists. If so, it just uses a reference to the existing object. It can do that without harm because there’s no way to change an immutable. This is why even two independently created strings may point to the same object:

    >>> a = "foo"
    >>> b = "foo"
    >>> a is b
    True
    

    But if var2 was some mutable sequential object, like a list, then var2[:] would be a shallow copy of var2, so that making changes to one would not affect the other.

    >>> a = list("foo")
    >>> a
    ['f', 'o', 'o']
    >>> b = a
    >>> b is a
    True
    >>> c = a[:]
    >>> c is a
    False
    >>> b.pop()
    'o'
    >>> a
    ['f', 'o']
    >>> b
    ['f', 'o']
    >>> c
    ['f', 'o', 'o']
    

    For the full picture, also read Ashwini Chaudharys answer.

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

Sidebar

Related Questions

Have a look at this Python code: a = [1, 2, 3] b =
I have started to look into python and am trying to grasp new things
I often see a pattern used in circumstances where we have look-up code that
Have a look here: In the following code, what would be the type of
Have a look at the following code you are not required to read the
I have several strings which look like the following: <some_text> TAG[<some_text>@11.22.33.44] <some_text> I want
I have some code to interface Python to C++ which works fine but every
I have two String.printable mysteries in the one question. First, in Python 2.6: >>>
Does C# have anything like Python's __getattr__ ? I have a class with many
To all: I have a question about converting from string to float 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.