Page 116 - TrackpadV5.1_class8
P. 116
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. 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])
114 Pro (V5.1)-VIII

