Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 151 - CodePilot V5.0 C8
P. 151

    USING ESCAPE SEQUENCES WITH STRINGS
                     An escape character or escape sequence is a character followed by a backslash (\). Escape

                     sequences allow you to include special characters such as a backslash (\) or quotes in strings. To
                     do this, simply add a backslash (\) before the character
                     you want to escape.
                     If you add any special character, such as an apostrophe,
                                                                                         The quotation mark used for
                     backslash  or single  or double  quotes,  inside  a string         opening the string must match
                     without  using  escape  sequences,  you  will get  an error.      the closing one, otherwise it will
                     To include  an apostrophe,  you  need to precede  the                 be considered incorrect.
                     character with a backslash (\) followed by an apostrophe.

                      Program 7 A program to demonstrate the use of escape sequences.

                         Program7.py                                                       Output

                      File  Edit  Format   Run    Options   Window    Help              It's a beautiful day!

                      greeting = "It\'s a beautiful day!"
                      print(greeting)

                     Similarly, you can include a new line (\n), a horizontal tab (\t) and a backspace (\b).

                      Program 8 A program to demonstrate string formatting using escape sequences.

                         Program8.py                                                       Output
                      File  Edit  Format   Run    Options   Window    Help              Name:         Amit
                                                                                        Age:          13
                      student_detail = "Name:\tAmit\nAge:\t13"
                      print(student_detail)

                     ACCESSING CHARACTERS OF A STRING

                     In Python,  each character in a string has a position  called an  index. Indexing starts  from 0
                     (positive indexing) for the first character and -1 (negative indexing) for the last character. You
                     can access and process an individual character or a set of characters from a string also known

                     as Traversing.
                     There are two ways of indexing a string:

                         Positive indexing: Indexing starts from the left, with the first character at index 0.

                         Negative indexing: Indexing starts from the right, with the last character at index -1.

                                                                                                                      149
                                                                                                Step Ahead with Python

| 146 147 148 149 150 151 152 153 154 155 156 |