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

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

            11.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 am good. ' + 'How are you'
                 'Hello, How are you? I am good. How are you'
            11.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)
                 *
                 **
                 ***
                 ****

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


            11.1.7 Reverse of a String

            Now we are ready to write a program that generates a string having characters in the reversed sequence of the original
            string. We begin by initializing the reverse string reverseStr as the null string and concatenating the characters in
            the string str1 to the string reverseStr one by one, as shown below:

             >>> str1 = 'Hello'
             >>> reverseStr = ''
             >>> for i in range(len(str1)):
             ...     reverseStr = str1[i] + reverseStr
             ...
             >>> print(reverseStr)
             >>> olleH
            It is worth noting that in the above code snippet, for loop uses indexes 0, 1,..., length (n)-1. Thus, when we execute the
            code, len('Hello') being 5, the variable i takes values 0, 1, 2, 3, 4.



                                                                                                       Strings   259
   256   257   258   259   260   261   262   263   264   265   266