Page 98 - 2611_SmartGPT Pro V(5.0) C-8
P. 98
USING ESCAPE SEQUENCES WITH STRINGS
Escape sequences or characters are used to insert special characters that are not directly printable
or representable in a string in Python. They represent non-printable or special characters within
strings. 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 that does not represent itself when used inside a
character or string. It is typically used to specify actions such as special characters and formatting
actions. For example, 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 5: To print special symbols using escape sequences
Program5.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 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.
Syntax for traversing a string:
<name of the string> [index]
Program 6: To access different elements of a string
Program6.py Output
File Edit Format Run Options Window Help o
e
name = 'orange'
print(name[0])
print(name[-1])
96 Computer Science (V5.0)-VIII

