Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 113 - 2615_KEC
P. 113

    CREATING A SINGLE-LINE STRING

                     Single-line string can be created by enclosing characters inside single or double quotes. It is like
                     assigning a value to a variable.

                     Program 4: To create a single-line string

                         Program4.py
                      File  Edit  Format   Run    Options   Window    Help

                      first_name = 'Aadya'
                      print (first_name)
                      last_name = 'Kaushal'
                      print (last_name)
                      str = ' '
                      print (str)

                     CREATING MULTILINE STRINGS

                     A multiline string in Python begins and ends with either three single quotes or three double quotes.
                     Any quotes, tabs, or newlines in between the “triple quotes” are considered part of the string.

                     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.''')

                     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]

                                                                                       Functions, String and List in Python  111

| 108 109 110 111 112 113 114 115 116 117 118 |