Page 231 - Robotics and AI class 10
P. 231

The syntax used is:

            STRING[startindex:endindex+1:step]
            Where

               Startindex: is the index number from where you wish to begin the process of slicing.
                          It is optional to specify.

                          If not given then default index 0 in forward indexing and -1 in backward indexing.
               endindex:  is the index number till where you wish to end the process of slicing.
                          It is optional to specify.

                          If not given then default index lastindex in forward indexing and firstindex in backward indexing.
            Step:  ●  +ve integer in case of forward indexing

                  ●  -ve integer in case of backward indexing
                  ●  It is optional to specify.
                  ●  If not given then default is always step 1
            Some of the examples are:

            >>> TXT="PROGRAMMING IS AMAZING"

            >>> TXT[::]
            'PROGRAMMING IS AMAZING'
            >>> TXT[::1]
            'PROGRAMMING IS AMAZING'
            >>> TXT[::-1]

            'GNIZAMA SI GNIMMARGORP'
            >>> TXT[:7:1]
            'PROGRAM'
            >>> TXT [4::1]

            'RAMMING IS AMAZING'
            >>> TXT [3:7]
            'GRAM'
            >>> TXT [15:]
            'AMAZING'

            >>> TXT [-1:-10:-1]
            'GNIZAMA S'


                        Task                                                          #Coding & Computational Thinking



                 Give the output of the following string slicing: where S='EDUCATION':

                 a.  S[-9:-6]     b.  S[3:-3]      c.  S[7:-5:-1]      d.  S[:1:-2]






                                                         Introduction to Data and Programming with Python  229
   226   227   228   229   230   231   232   233   234   235   236