Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,057 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,576 people online right now. Registration is fast and FREE... Join Now!




Slices help

 
Reply to this topicStart new topic

Slices help

inc0mplete
4 Aug, 2008 - 06:55 PM
Post #1

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 22

Hello, I'm reading the documentation for python on the Python website. I don't really understand slices. I understand what they do...but not the second index.

for example
CODE
x = "helloA"
print x[0:2]
print x[2:4]


These are just examples I made up. I understand the first index 0 and 2 will be "h" and "l" because the indexing starts at 0. But what is the second index for? In the examples given, it's not starting from 0...eg. 0:2 does not produce "hl"

l would be the 3rd element or x[2]. Someone please help me understand the count. Thank you in advance.
User is offlineProfile CardPM
+Quote Post

linuxunil
RE: Slices Help
4 Aug, 2008 - 07:01 PM
Post #2

New D.I.C Head
Group Icon

Joined: 7 Mar, 2006
Posts: 46



Thanked: 2 times
Dream Kudos: 125
My Contributions
The second index is exclusive. In other words the index of the second one is not included in the slice. So x[2:4] is selecting the second and third indexes but not the fourth.

This post has been edited by linuxunil: 4 Aug, 2008 - 07:52 PM
User is offlineProfile CardPM
+Quote Post

inc0mplete
RE: Slices Help
4 Aug, 2008 - 07:13 PM
Post #3

New D.I.C Head
*

Joined: 10 May, 2008
Posts: 22

THANK YOU! but another fast question; what happens if it is x[1:1]? in the example i saw, lets say the same one i used above. if i did x[1:1] i would get "el". buy why? it's including the 1 index, and also excluding the 1 index? And if so, why is the 2nd index being sliced?

This post has been edited by inc0mplete: 4 Aug, 2008 - 07:16 PM
User is offlineProfile CardPM
+Quote Post

Nova Dragoon
RE: Slices Help
5 Aug, 2008 - 12:08 PM
Post #4

The Innocent Shall Suffer, Big Time
Group Icon

Joined: 16 Aug, 2001
Posts: 6,129



Thanked: 7 times
Dream Kudos: 515
Expert In: Python, Linux

My Contributions
CODE

>>> x = "helloA"
>>> x[1:1]
''


User is offlineProfile CardPM
+Quote Post

linuxunil
RE: Slices Help
7 Aug, 2008 - 06:08 AM
Post #5

New D.I.C Head
Group Icon

Joined: 7 Mar, 2006
Posts: 46



Thanked: 2 times
Dream Kudos: 125
My Contributions
It's just the way they decided to implement slices, the second index is always excluded.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 06:04PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month