Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 121 - 2615_KEC
P. 121

    CREATING A SINGLE-LINE STRINGS
                     Single-line strings can be created by enclosing characters inside single or double quotes. It is like
                     assigning a value to a variable.

                     Program 15: To create a single-line string.

                         Program15.py
                      File  Edit  Format  Run    Options  Window    Help

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

                     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 16: To print multiline string.
                         Program16.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.''')

                         Output

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

                     STRING OPERATORS
                     There are two basic string operators in Python, + and *. Python uses + for concatenation and * for
                     replication. The description of these operators are as follows:

                       String Concatenation Operator (+): String concatenation operator joins two or more strings
                       into one.

                                                                                            Control Structures in Python  119

| 116 117 118 119 120 121 122 123 124 125 126 |