Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 160 - 2632_CodeMind
P. 160

    Program 7: To access different elements of a string.

                          Program7.py
                       File  Edit  Format    Run   Options   Window    Help
                                                                                    Output
                       name = 'orange'
                       print(name[0])                                             o
                       print(name[-1])                                            e

                          String Operators

                     There are two basic string operators in Python, + and *. Python uses + for concatenation and * for
                     replication.

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

                      Program 8: To join two or more strings using string concatenation operator.

                          Program8.py
                       File  Edit  Format   Run    Options   Window    Help

                       s1 = 'Here'
                       s2 = 'Is'
                       s3 = 'Python'
                       s = s1 + s2 + s3                                             Output
                       print(s)                                                   HereIsPython

                       String Replication Operator (*): The replication operator is used to repeat the string for a given
                       number of times.

                      Program 9: To repeat a string using string replication operator.

                         Program9.py
                      File  Edit  Format   Run    Options   Window    Help

                      s1 = 'Hello'
                      s = s1 * 3                                                   Output
                      print(s)                                                   HelloHelloHello

                                                                                          Functions and String in Python  159

| 155 156 157 158 159 160 161 162 163 164 165 |