The basic syntax for the slice operator is [start:end:step]
, where end
index is excluded. If any of those parameters are missing, default value is used.
For example,
some_string[:len(some_string)] # from 0 to the end of string(step=1 by default)
some_string[:len(some_string):1] # identical to the previous example
some_string[0:len(some_string):1] # also identical to the previous example
some_string[0:] # identical as well(end is len(some_string) by default here)
some_string[0::1] # identical to previous example
As you can see, default values will be used if not provided, which is very useful. I bet all of us would rather write [:]
than [0:len(string):1]
P.S
This rule changes slightly if your len(string)-1
and other operators are missing. For example,
some_string[len(string)-1:0] # the default value for step will be -1