Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 159 - 2632_CodeMind
P. 159

    You will get the following output:

                          Output

                       A sequence of
                       characters are called a string.
                       Strings are used by programming
                       languages to manipulate text such
                       as words and sentences.

                           Using Escape Sequences with Strings

                      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. For
                      example, '\t' for tab, '\n' for new line, and '\r' is for carriage return.

                       Program 6: To print special symbols using escape sequences.

                          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. Syntax for traversing a string:

                          <name of the string> [index]

                      158   Premium Edition-VIII

| 154 155 156 157 158 159 160 161 162 163 164 |