[Tutor] Strings.

Alexander rhettnaxel at gmail.com
Wed Oct 24 01:48:27 CEST 2012


On Tue, Oct 23, 2012 at 7:24 PM, Nitin Ainani <nitinainani at gmail.com> 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
>
> emptyString = ""
#consider the string to be non-empty, that is, let's say 13 characters
long (the number you chose below)
string13 = "1234567890ABC"
print ( string13 [ 0 ] )

>Now if we write following statement
>
> print (  s [ 0 ]  )      # it gives error
***what error? ***
The error and your example can be used to understand indexes.
 s [ 0 ] is asking for the value of a specific piece of s.
s[0] is asking for the value at index Zero of string s.
S has nothing inside of it.

>
> print(s[0:])    # it does not give error
#what does it give?
> print (s[13:13])   # this too does not give error
#What is the output
> why?

And with your other two examples I suggest understanding slicing.
slice! I want the whole pie, myPie [ 0: ]  #the : around an index has
a special meaning.
Try a string with text and observe the output of your questions.
aString [ : ]
aString [ : 0 ]
aStr [ 1: ]

etc
Check it out, mess around, get back to me.
Alexander


More information about the Tutor mailing list