Weired behaviour - The slice of empty string not return an error

MRAB python at mrabarnett.plus.com
Sun Oct 30 23:05:22 EDT 2022


On 2022-10-30 14:57, Yassine Nasri wrote:
> Hello,
> 
> len('')  # => 0''[0]    # => error''[::]   # => ''''[::]   # <=> ''[0:len(''):1]
> 
> the syntax of slice:
> 
> slice(start, end, step)
> 
> The start in ''[::] equivalent at index 0 of the ''
> 
> Since the index 0 of '' returns an error.
> 
> The ''[::] should not return an error logically.
> 
Python uses half-open ranges. It excludes the end.

''[0:0:1] starts at index 0 but ends at and excludes index 0.


More information about the Python-list mailing list