Page 68 - tp_Modula_v2.0
P. 68
Using Escape Sequences
Escape sequences or characters are used to insert special characters that are invalid in Python.
Quotes are special characters and to insert them in a string we must escape them with a character
in front of them.
An escape sequence is a sequence of characters which does not get displayed in output when
used inside a character or a 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. It is used to represent white space characters, for example, ‘\t’ for tab, and
‘\n’ for new line.
Program 4: To print special symbols using escape sequences.
Program4.py
File Edit Format Run Options Window Help
print("Orange\n Education is a reputed \t Edtech Company.")
On running the above program, you will get the following output:
Output
Orange
Education is a reputed Edtech Company.
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 a list:
Index with positive integers: Index from left starts with 0.
Index with negative integers: Index from right starts with -1.
Syntax for traversing a string:
<name of the string> [index]
Program 5: To access different elements of a string.
Program5.py
File Edit Format Run Options Window Help
name = "Orange"
print(name[1])
print(name[-1])
66 Touchpad MODULAR (Version 2.0)

