Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 122 - 2615_KEC
P. 122

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

                          Program17.py
                       File  Edit  Format   Run   Options  Window    Help

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

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

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

                          Program18.py
                       File  Edit  Format   Run   Options  Window    Help
                                                                                    Output
                       s1 = 'Hello'
                       s = s1 * 3                                                 HelloHelloHello
                       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 of using len() function is: len(string_name)
                         lower(): The lower() function converts all uppercase letters to lowercase. Syntax of using lower()
                        function is: string_name.lower()
                         upper(): The upper() function  converts all  lowercase  letters  to  uppercase.  Syntax  of using

                        upper() function is: string_name.upper()
                         capitalize(): The capitalize() function returns a string with the first character in capital. Syntax
                        of using capitalize() function is: string_name.capitalize()
                      Program 19: To use various string built-in functions.

                           Program19.py

                        File  Edit  Format   Run   Options   Window    Help
                        str = 'Hello'                                                       Output
                        print(len(str))                                                   5
                        print('Hello'.lower())                                            hello
                        print('hello'.upper())                                            HELLO
                        print('hello'.capitalize())
                                                                                          Hello

                      120   Premium Edition-VII

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