Page 258 - Computer Science Class 11 Without Functions
P. 258
11 STRINGS
Chapter Outline
11.1 Strings 11.2 Membership Operator in
11.3 String Slices 11.4 Constructing a New String by Replacing Parts of a String
11.5 String Methods 11.6 Case Study
Introduction
Data types such as int, float, complex, and bool are called scalar data. In addition to the aforementioned scalar data
types, we have already seen examples of strings. For instance, we used strings to document our programs and produce
outputs. In this chapter, we will learn more about strings.
11.1 Strings
Python built-in type str enables us to deal with the strings in a Python program. A str instance is a string of text
enclosed between single or double quotes. In this book, we prefer to use single quotes. Next, we give some examples
of strings:
>>> 'Block-A, HouseNo #177, Pitampura, Delhi-110034'
'Block-A, HouseNo #177, Pitampura, Delhi-110034'
>>> 'shyam@gmail.com'
'shyam@gmail.com'
>>> 'Hello"How are you?"I am good, how are you?'
'Hello"How are you?"I am good, how are you?'
>>> print('Hello\nHow are you?\nI am good, how are you?')
Hello
How are you?
I am good, how are you?
A str instance is a string of text enclosed between single or double quotes.
Note that a string may include special characters. The preceding example illustrates when a string includes the character
sequence \n, the print() function transfers the print control to the next line for each occurrence of the character
sequence \n. When a string is enclosed between single quotes, a double quote mark may be included as part of the
string. However, to include a single quote ('), it should be preceded by a backslash (\) character. For example,
256 Touchpad Computer Science-XI

