String slices/substrings/ranges in Python -


i'm newbie in python , know found curious.

let's have this:

s = "hello" 

then:

s[1:4] prints "ell" makes sense... , s[3:-1] prints 'l' makes sense too..

but!

s[-1:3] same range backwards returns empty string ''... , s[1:10] or s[1:-20] not throwing error @ all.. which.. point of view, should produce error right? typical out-of-bounds error.. :s

my conclusion range left right, confirm community if i'm saying or not.

thanks!

s[-1:3] returns empty string because there nothing in range. requesting range last character, third character, moving right, last character past third character.

ranges default left right.

there extended slices can reverse step, or change it's size. s[-1:3:-1] give 'o'. last -1 in slice telling slice should move right left.

slices won't throw errors if request range isn't in string, return empty string positions.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -