Page 100 - KEC Khaitan C8 Flipbook
P. 100

Program 5: To print multiline string

                      Program5.py
                   File  Edit  Format    Run   Options   Window    Help

                    print('''A sequence of
                    characters are called a string.
                    Strings are used by programming
                    languages to manipulate text such
                    as words and sentences.''')




                  You will get the following output:

                      Output

                   A sequence of
                   characters are called a string.
                   Strings are used by programming
                   languages to manipulate text such
                   as words and sentences.




                  TRAVERSING A STRING
                  Traversing means visiting each element and processing it as required by the program. We can
                  access the characters of a string one at a time using indexing.

                  There are two ways of indexing:
                     Positive Indexing: Index from left starts with 0.

                     Negative Indexing: Index from right starts with -1.
                  The syntax for traversing a string is as follows:

                    <name of the string> [index]

                  Program 6: To access different elements of a string

                      Program6.py
                   File  Edit  Format    Run   Options   Window    Help

                   name = 'orange'
                   print(name[0])
                   print(name[-1])





                      Output

                   o
                   e






                   98   Premium Edition-VIII
   95   96   97   98   99   100   101   102   103   104   105