Page 349 - Computer Science V2.0 Class 11
P. 349

>>> '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
                 string, we precede it by 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 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?

                         (a)  print('''Python!\n
                             Interpreted Programming Language''')


                 12.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,




                                                                                                            Strings   335
   344   345   346   347   348   349   350   351   352   353   354