Page 229 - Robotics and AI class 10
P. 229

'10 B'

              >>> 1+2
              3
              >>> '1'+'2'
              '12'
              >>> '1'+
              TypeError: can only concatenate str (not "int") to str
            2.   Multiplication Operator ‘*’ is also known as the Repetition operator for the string values. It works with string
               value on one side and with always integer on the other side of the operator.
               For example:

              >>> 'hi'*3
              'hihihi'
              >>> 4*'a'
              'aaaa'
              >>> 3*'hello'*2
              'hellohellohellohellohellohello'
              >>> 1*'d'
              'd'
              >>> 0*'d'
              ''
              >>> 2*'#'*3

              '######'
              >>> "2" *"#"
              TypeError: can't multiply sequence by non-int of type 'str'

            Membership Operators
            Python supports two membership operators: in and not in:

                   Operators              Purpose                        Example                    Output

                in               Returns  True if a substring   >>> 'el' in 'Delhi'            True
                                 exists in a given string     True

                                                              >>> 'le' in 'Delhi'              False
                                                              False
                                                              >>> 'e' in 'aeiou'               True
                                                              True
                                                              >>> 'E' in 'aeiou'               False
                                                              False
                not in           Returns  True if a substring  >>> 'is' not in 'Life is  False
                                 does not exists in a given  beautiful'
                                 string                       False
                                                                                               True
                                                              >>> 'Bea' not in 'Life is
                                                              beautiful'
                                                              True



                                                         Introduction to Data and Programming with Python  227
   224   225   226   227   228   229   230   231   232   233   234