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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:06:06+00:00 2026-06-17T13:06:06+00:00

I am just learning python and I am going though the tutorials on https://developers.google.com/edu/python/strings

  • 0

I am just learning python and I am going though the tutorials on https://developers.google.com/edu/python/strings

Under the String Slices section

s[:] is ‘Hello’ — omitting both always gives us a copy of the whole
thing (this is the pythonic way to copy a sequence like a string or
list)

Out of curiosity why wouldn’t you just use an = operator?

s = 'hello';
bar = s[:] 
foo = s 

As far as I can tell both bar and foo have the same value.

  • 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-17T13:06:08+00:00Added an answer on June 17, 2026 at 1:06 pm

    = makes a reference, by using [:] you create a copy. For strings, which are immutable, this doesn’t really matter, but for lists etc. it is crucial.

    >>> s = 'hello'
    >>> t1 = s
    >>> t2 = s[:]
    >>> print s, t1, t2
    hello hello hello
    >>> s = 'good bye'
    >>> print s, t1, t2
    good bye hello hello
    

    but:

    >>> li1 = [1,2]
    >>> li = [1,2]
    >>> li1 = li
    >>> li2 = li[:]
    >>> print li, li1, li2
    [1, 2] [1, 2] [1, 2]
    >>> li[0] = 0
    >>> print li, li1, li2
    [0, 2] [0, 2] [1, 2]
    

    So why use it when dealing with strings? The built-in strings are immutable, but whenever you write a library function expecting a string, a user might give you something that “looks like a string” and “behaves like a string”, but is a custom type. This type might be mutable, so it’s better to take care of that.

    Such a type might look like:

    class MutableString(object):
        def __init__(self, s):
            self._characters = [c for c in s]
    
        def __str__(self):
            return "".join(self._characters)
    
        def __repr__(self):
            return "MutableString(\"%s\")" % str(self)
    
        def __getattr__(self, name):
            return str(self).__getattribute__(name)
    
        def __len__(self):
            return len(self._characters)
    
        def __getitem__(self, index):
            return self._characters[index]
    
        def __setitem__(self, index, value):
            self._characters[index] = value
    
        def __getslice__(self, start, end=-1, stride=1):
            return str(self)[start:end:stride]
    
    
    if __name__ == "__main__":
        m = MutableString("Hello")
        print m
        print len(m)
        print m.find("o")
        print m.find("x")
        print m.replace("e", "a") #translate to german ;-)
        print m
        print m[3]
        m[1] = "a"
        print m
        print m[:]
    
        copy1 = m
        copy2 = m[:]
        print m, copy1, copy2
        m[1] = "X"
        print m, copy1, copy2
    

    Disclaimer: This is just a sample to show how it could work and to motivate the use of [:]. It is untested, incomplete and probably horribly performant

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

Sidebar

Related Questions

I am just learning python. I read about Unicode characters and strings and was
Just learning Python. Reading through the official tutorials. I ran across this: While appends
I just start learning Python + Tornado for my web servers. Every time I
I have just started learning python version 3 and trying to create a file
So ive just started learning python on WAMP, ive got the results of a
I'm just learning about python. I'm fairly new. I have the following code that
I just started using/learning Python and have some questions. I have a text file
Hi thanks in advance for your help. I'm just getting started learning Python and
I am a newbie to python and just learning things as i do my
I am just learning how to do unit-testing. I'm on Python / nose /

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.