[Tutor] Doubt!

Dave Angel d at davea.name
Wed Oct 24 01:39:10 CEST 2012


On 10/23/2012 07:14 PM, Nitin Ainani wrote:
> Dear Sir/Madam,
>
> I am  new to python I have a question. It is as follows:
>
> Suppose *s* is a variable and *s* stores empty string
>
> s=""
> Now if we write following statement
>
>
> print(s[0])      # it gives error

There is no 0th character, so it's an error.  If they had wanted to
avoid an error, they'd have had to return None or some other flag, and
that's not pythonic.

>
> print(s[0:])    # it does not give error

A slice is more tolerant, by definition.  It gives you all the items in
the range you specify, and the resulting string (in this case) is equal
to the original one.

> why?
>
>

-- 

DaveA



More information about the Tutor mailing list