Page 303 - Computer Science Class 11 With Functions
P. 303

12                                                                          STRINGS














              Chapter Outline


              12.1 String                                        12.2 String Slices
              12.3 String Methods                                12.4 Case Study






            Introduction

            Data types such as int, float, complex, and bool are called scalar data.  In addition to the aforementioned scalar data
            types, we have already seen examples of strings. For instance, we used strings to document our programs and produce
            outputs. In this chapter, we will learn more about strings.

            12.1 String

            Python built-in type str enables us to deal with the strings in a Python program. A str instance is a string of text
            enclosed between single or double quotes. In this book, we prefer to use single quotes. Next, we give some examples
            of strings:
             >>> 'Block-A, HouseNo #177, Pitampura, Delhi-110034'
                 'Block-A, HouseNo #177, Pitampura, Delhi-110034'
             >>> 'shyam@gmail.com'
                 'shyam@gmail.com'
             >>> 'Hello"How are you?"I am good, how are you?'
                 'Hello"How are you?"I am good, how are you?'
             >>> print('Hello\nHow are you?\nI am good, how are you?')
                 Hello
                 How are you?
                 I am good, how are you?



                   A str instance is a string of text enclosed between single or double quotes.


            Note  that  a  string  may  include  special  characters.  The  preceding  example  illustrates,  when  a  string  includes  the
            character sequence \n, the print() function transfers the print control to the next line for each occurrence of the
            character sequence \n. When a string is enclosed between single quotes, a double quote mark may be included as part
            of the string. However, to include a single quote ('), it should be preceded by a backslash (\) character. For example,



                                                                                                       Strings   301
   298   299   300   301   302   303   304   305   306   307   308