Page 228 - Robotics and AI class 10
P. 228

Escape Sequences
        An escape sequence is a sequence of Non-Printable Characters with special meaning. It represents characters
        that cannot be directly written in a string. They are denoted by a backslash (\) followed by a specific character or
        sequence.

        Some of the escape characters used with Strings are:

           Symbol            Purpose                        Example                          Output
            \"       Inserts double quotation   s="hello \"WORLD\""                Hello “WORLD”
                     in a string               print(s)
           \\        Inserts backslash in a    print("we  use\\[backslash] We use\(backslash) as an
                     string                    as an escape sequence')             escape sequence
           \n        Creates a Newline in a    x="life\nis\nbeautiful"             Life
                     string                    print(x)                            Is
                                                                                   Beautiful

           \t        Creates   a   tab  space msg="hello\tClasss"                  hello class
                     equivalent to 6 normal  print(msg)
                     spaces.
           \'        Inserts a  single quotation  s='How\s you new bicycle?'       How’s your new bicycle?
                     in a string               print(s)



        String Operations
        Some of the basic operations allowed on a string datatype in Python are listed below:


        ‘+’ and ‘*’ Operators used with String
        String data type allows only two mathematical operator: addition '+' and multiplication '*' to be used.
        These two mathematical operators work differently with the string values.
        1.   Addition operator ‘+’ is known as Concatenation operator with the string values as it works to join the string
           values.
           For example:

          >>> 'Hello'+'class'
          'Helloclass'
          >>> S="LIFE"
          >>> T="IS"
          >>> U="BEAUTIFUL"
          >>> S+T+U

          'LIFEISBEAUTIFUL'
          >>> S+" "+T+" "+U
          'LIFE IS BEAUTIFUL'
          >>> SEC="B"
          >>> CL="10"
          >>> CL+' '+SEC




              226     Touchpad Robotics & Artificial Intelligence-X
   223   224   225   226   227   228   229   230   231   232   233