3
sentence = "Hello"
print sentence
print sentence[:]

Both outputs the same thing, i.e. Hello

So, when and why to use/not use [:] ?

Thanks! :)

Desmond Yeoh
  • 57
  • 1
  • 2
  • 4
    Get information about that here: http://stackoverflow.com/questions/509211/explain-pythons-slice-notation – Nobi Feb 09 '15 at 06:59
  • No effort from your side, it seems you didn't even research what this does. – Ulrich Eckhardt Feb 09 '15 at 07:18
  • 2
    @UlrichEckhardt I'm really sorry... but this is my first post in stackoverflow... and i don't know it's called the slicing notation, i did have problem searching for [:] in google search. :) – Desmond Yeoh Feb 09 '15 at 14:47

3 Answers3

8

As Nobi pointed out in the comments, there's already a question regarding Python's slicing notation. As stated in the answer to that question, the slicing without start and end values ([:]) basically creates a copy of the original sequence.

However, you have hit a special case with strings. Since strings are immutable, it makes no sense to create a copy of a string. Since you won't be able to modify any instance of the string, there's no need to have more than one in memory. So, basically, with s[:] (being s a string) you're not creating a copy of the string; that statement is returning the very same string referenced by s. An easy way to see this is by using the id() (object identity) function:

>>> l1 = [1, 2, 3]
>>> l2 = l1[:]
>>> id(l1)
3075103852L
>>> id(l2)
3072580172L

Identities are different. However, with strings:

>>> s1 = "Hello"
>>> s2 = s1[:]
>>> id(s1)
3072585984L
>>> id(s2)
3072585984L

Identity is the same, meaning both are the same exact object.

Community
  • 1
  • 1
Pablo Antonio
  • 859
  • 2
  • 8
  • 20
1
>>> a = [1, 2, 3]
>>> b=a[:]
>>> id(b)
4387312200
>>> id(a)
4387379464

When you want to make a deep copy of an array.

>>> a='123'
>>> b=a[:]
>>> id(a)
4387372528
>>> id(b)
4387372528

But since string is immutable, string[:] has no difference with string itself.

P.S. I see most of people answering this question didn't understand what is the question at all.

John Hua
  • 1,400
  • 9
  • 15
  • 2
    This makes a *shallow* copy of the list, not a deep copy. You can confirm this by noticing that `a = [[]]`; b = a[:]; a[0] is b[0]` gives True. To do a true deep copy, use `copy.deepcopy`](https://docs.python.org/3.4/library/copy.html#copy.deepcopy). – lvc Feb 09 '15 at 07:22
  • 1
    ... no, no they don't. `a = []; b = []; a is b` is False. And conversely, you can put whatever you want into the inner list and get the same result - `mylist[:]` does *not* make copies of any of the objects in the list. Try `a = [['a', 'b', 'c', 'd']]; b = a[:]; print(a[0] is b[0]); a[0].append('e'); print(b[0])`. – lvc Feb 09 '15 at 07:37
0

Thee reason why you are getting Hello as output, is you are not passing any parameter.

L[start:stop:step]

Here L is your variable, which holds Hello. and start means the initial position of the string and stop means where you want to end your string with & step means how many char you want to skip.

For more information on this topic, visit this

See, if that resolved your issue.

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39