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

12.1.4 Length of a String
                 To find the length of a string, Python provides a the built-in function len() which returns the length of a string. For
                 example,

                  >>> len('hello')
                      5
                  >>> len('hello\nhow are you')
                      17
                  >>> len('hello\n\n\nhow are you')
                      19
                 As the character sequence \n denotes a single end-of-line character, it is counted only once while computing the
                 length of the string.
                 12.1.5 Concatenation of Strings

                 The + operator is used to concatenate two strings. For example,
                  >>> '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 a good. ' + 'How are you'
                      'Hello, How are you? I am good. How are you'
                 12.1.6 Multiplication Operator
                 The multiplication operator (*) produces a string, concatenated with itself a specified number of times. For instance,
                 the expression 'Hello'*5  yields the string 'HelloHelloHelloHelloHello', i.e. the string 'Hello'
                 repeated five times (second operand:5). As expected, the expression 'Hello'*0 yields an empty string as the string
                 'Hello' is repeated zero times.

                  >>> 'Hello'*5
                      'HelloHelloHelloHelloHello'
                  >>> 'Hello'*0
                      ''
                 The multiplication operator is quite useful when we want to print a pattern. For example, to print a 4-line pattern with
                 a single * in the first line, two *'s in the second line, three *'s in the third line, and finally, four *'s in the fourth line,
                 we simply invoke the print() function as follows:
                  >>> print('*' + '\n' + '*'*2 + '\n' + '*' * 3 + '\n' + '*' * 4)
                      *
                      **
                      ***
                      ****


                        len(): Returns the length of a string.
                        Operator +: Used to concatenate two strings.
                        Operator *: Concatenates a string with itself, a specified number of times.


                 12.1.7 Reverse of a String

                 Now we are ready to write a function reverse(str1) that generates a string having characters in the reversed sequence
                 of the original string; for example,

                  >>> reverse('excellent')
                      'tnellecxe'
                  >>> reverse('tnellecxe')
                      'excellent'




                                                                                                            Strings   337
   346   347   348   349   350   351   352   353   354   355   356