Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 137 - 2609_Logicode
P. 137

    Program 7: To repeat a string using string replication operator

                         Program7.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)
                       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 8: To use various string built-in functions

                         Program8.py
                      File  Edit  Format   Run    Options   Window    Help

                      str = 'Hello'
                      print(len(str))
                      print('Hello'.lower())
                      print('hello'.upper())
                      print('hello'.capitalize())

                         Output
                      5
                      hello
                      HELLO
                      Hello

                                                                                               String and List in Python  135

| 132 133 134 135 136 137 138 139 140 141 142 |