Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 114 - 2615_KEC
P. 114

    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])

                      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.

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

                          Program7.py
                       File  Edit  Format    Run   Options   Window    Help

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

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

                      Program 8: To repeat a string using string replication operator

                          Program8.py
                       File  Edit  Format    Run   Options   Window    Help

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

                      STRING BUILT-IN FUNCTIONS
                      Python includes the following built-in functions to manipulate strings:
                          len(): The len() function calculates and returns the length of a string supplied as an argument.
                        Syntax:
                        len(string_name)

                       112  Premium Edition-VIII

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