Page 90 - TP_V5.1_C8_fb
P. 90

Program 7: To access different elements of a string.

                       Program7.py
                    File  Edit  Format  Run    Options  Window    Help

                    name = 'orange'                                            Output
                    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. The description of these operators areas follows:

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

                  Program 8: To join two or more strings using the 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 the string replication operator.

                       Program9.py
                    File  Edit  Format  Run    Options  Window    Help

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




                  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 of using len() function is:

                    len(string_name)


                   88   Premium Edition-VIII
   85   86   87   88   89   90   91   92   93   94   95