On Wed, March 22, 2006 12:11 pm, Kaushal Shriyan wrote: > Hi > > I am new to python and I am going through the URL > http://www.ibiblio.org/obp/thinkCSpy/index.htm, At the moment > I am going through the Chapter 7 Strings, I am stuck with understanding > slice which is a part of a String, I am not able to understand how it > functions
Can you be a bit more specific? String slices are copies of pieces of strings. As for the values of indices, take a look at http://nibrahim.net.in/slice.png It's something I tried to use during a presentation at my workplace. You create something like that by doing foo = "corrupt" Then the indices are as per the diagram. If you say foo[0], you'll get back "c". foo[1] will give you "o" etc. It helps to think of the indices as pointing to in between the letters. Not to the letters themselves. Now, try something like foo[1:3], you'll get back "or" and if you refer the diagram, it's fairly straightforward why (slice starts at position 1 and ends at position 3. Everything in between is displayed). foo [:3] is the same as saying foo[0:3]. foo[3:] is similar. Negative indices start from the position shown. There's a bit of asymmetry here since foo[0] refers to the first element of the string but foo[-1] refers to the last one. You'll get used to it once you get used to the idea of indices pointing to in between the elements though. Another thing you should understand (python experts, please correct me if I'm wrong) is that the slice operator actually creates a new string and doesn't return a piece of the existing string. So, when you say foo[:], you'll get a string that's a copy of the original one. Please let me know if I can help you out with this anymore. -- -NI _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor