Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 161 - 2632_CodeMind
P. 161

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

                          Program10.py

                       File  Edit  Format   Run    Options   Window    Help

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

                      You will get the following output:

                          Output

                       5
                       hello
                       HELLO
                       Hello

                              Let’s CatCh uP

                        1.  What is a string?
                           ____________________________________________________________________________________

                        2.  What are escape sequences?
                           ____________________________________________________________________________________

                      160   Premium Edition-VIII

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