Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 96 - 2612_Digimind
P. 96

    USING ESCAPE SEQUENCES WITH STRING
                      Escape sequences or characters are used to represent special characters that cannot be directly

                      inserted into a string.
                      An escape sequence is a sequence of characters that does not represent itself when used inside
                      a  character  or string.  It is  typically  used  to  specify  actions  such  as  carriage  returns and  tab
                      movements. The backslash (\) is a special character and is also known as the escape character in

                      Python. To include special characters like quotes within a string, you need to escape them with a
                      backlash. It is used to represent white space characters, for example, '\t' for tab, '\n' for new line
                      and '\r' is for carriage return.

                      Program 6: To demonstrate the use of escape character

                          Program6.py
                       File  Edit  Format    Run   Options   Window    Help

                       print('I love programming in \n Python')

                      You will get the following output:

                          Output

                       I love programming in
                        Python

                      TRAVERSING A STRING

                      Traversing means visiting each element and processing it as required by the program. We can
                      access the characters of a string one at a time using indexing.

                      There are two ways of indexing:
                         Positive Indexing: Index from left starts with 0.

                         Negative Indexing: Index from right starts with -1.

                      The syntax for traversing a string is as follows:

                        <name of the string> [index]
                      Program 7: To access different elements of a string

                          Program7.py
                       File  Edit  Format    Run   Options   Window    Help

                       name = 'orange'
                       print(name[0])
                       print(name[-1])

                       94   Premium Edition-VIII

| 91 92 93 94 95 96 97 98 99 100 101 |