How function something_something [ x : x ] works


#1

Hello everyone.
I have a question .. For example lets take word "anakin" where positions are a=0 n=1 etc..
When I will use s = "anakin" and then print s[1:5] I will have letters between position 1 and 5 = naki
but when I use print s[1:len(s)] i have nakin .. Isnt this the reason why in python we start from position 0?


#2

"Then we access a slice of "Charlie" using s[1:4]. This returns everything from the letter at position 1 up till position 4. (har) "
Why the first position is included (h) but fourth (l) isn't. If s[x,y] why x is always included but y not.


#3

but len() is one indexed based, the length of anakin is 6. so that is why you get all letters.

I always see index slices (like you are using here) as a loop:

for(i = 0; i < "Charlie".length; i++){
  
}

i know, this is not python syntax. But it explains why the start value is included, and the stop value is not, because < (lesser then) is used, causing the stop value not to be included


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.