Page 259 - Computer Science Class 11 Without Functions
P. 259

>>> 'Hello"How are you?"I am good, how are you?'
                 'Hello"How are you?"I am good, how are you?'
             >>> 'Hello\'How are you?\'I am good, how are you?'
                 'Hello'How are you?'I am good, how are you?'
            Similarly, if a string is enclosed between the double quote marks, then to include the double quote mark (") in the
            sting, we precede it with a backslash symbol (\"), for example,

             >>> "Hello \"How are you?\"I am good, how are you?"
                 'Hello "How are you?"I am good, how are you?'
            Although the backslash has a special meaning, its meaning can be overridden, and it can be included in a string by
            preceding it with another backslash as shown below:

             >>> 'abc\\def\\ghi'
                 'abc\\def\\ghi'
             >>> print('abc\\def\\ghi')
                 abc\def\ghi
            Python allows a string literal to span multiple lines. A multi-line string is enclosed between triple single quotes or triple
            double quotes, for example,

             >>> '''Hello, how are you?
             ... I am good.
             ... How are you?'''
                 'Hello, how are you?\nI am good.\nHow are you?'
            However, sometimes we may like to put parts of the text on different lines for enhanced readability but may not
            want to split the text into multiple lines while displaying it. For this purpose, we terminate each line with a backslash
            character (also known as a line continuation character), as shown below:

             >>> 'Hello, How are you?\
             ... I am good.\
             ... How are you?'
             >>> 'Hello,How are you?I am good.How are you?'



                     What will be displayed on executing the following statement?
                     print('''Python!\n Interpreted Programming Language''')



            11.1.1 Indexing


            Position of a character in a string is called its index, for example, in the string 'hello', the characters 'h', 'e',
            'l', 'l', and 'o' are stored at index 0, 1, 2, 3, 4, respectively.  To access a character in a string, the
            string object is followed by an opening square bracket, the index to be accessed, and a closing square bracket, as
            shown below:
             >>> greet = 'hello'
             >>> greet[0]
                 'h'
             >>> greet[4]
                 'o'


                  To access a character in a string, use the index of the required element within square brackets.


            Another way to access the characters in a string is to use the negative indices -1, -2, -3,... the last character of the string

            being at index -1. For example,
             >>> print (greet[–5], greet[–4], greet[–3], greet[–2], greet[–1])
                 h e l l o


                                                                                                       Strings   257
   254   255   256   257   258   259   260   261   262   263   264