MBOSE Class XI Computer Science · Chapter 8
Strings
Opening soonHow Python stores text, how to pick out pieces with an index or a slice, how to loop over a string, and how the string methods change and test text.
15 lessons · 71 practice questions
What this chapter covers
- Strings: introduction, string operations (concatenation, repetition, membership and slicing), traversing a string using loops, built-in functions and methods
len(),capitalize(),title(),lower(),upper(),count(),find(),index(),endswith(),startswith(),isalnum(),isalpha(),isdigit(),islower(),isupper(),isspace(),lstrip(),rstrip(),strip(),replace(),join(),partition(),split() - Suggested string programs
Key terms
- String
Immutable sequence of characters
- Index
Position number of a character; the first is 0
- Slice
A part of a string,
s[n:m:k]- Concatenation
Joining strings with
+- Repetition
Repeating a string with
*- Traversal
Visiting each character in turn
- Method
A function called with a dot, such as
s.upper()- Immutable
Cannot be changed after creation
- Palindrome
A string that reads the same forwards and backwards
Important questions with answers
10 of the 71 questions on this chapter, solved.
Question 1 · Multiple choice · 1 mark
The output of "Python"[1:4] is
- (a) Pyt
- (b) yth
- (c) ytho
- (d) Pyth
Answer: (b) yth
Indices 1, 2, 3.
Question 2 · Multiple choice · 1 mark
len("Hi there") is
- (a) 7
- (b) 9
- (c) 8
- (d) 6
Answer: (c) 8
The space is counted.
Question 3 · Multiple choice · 1 mark
"banana".replace("a", "o") is
- (a) bonono
- (b) banana
- (c) bonana
- (d) ononon
Answer: (a) bonono
Every a becomes o.
Question 4 · Multiple choice · 1 mark
"123".isdigit() is
- (a) an error
- (b) False
- (c) None
- (d) True
Answer: (d) True
All characters are digits.
Question 5 · Assertion and reason · 1 mark
Assertion (A): The statement s[0] = 'J' raises an error for a string s.
Reason (R): Strings are immutable.
- (a) Both A and R are true, and R is the correct explanation of A.
- (b) Both A and R are true, but R is not the correct explanation of A.
- (c) A is true, but R is false.
- (d) A is false, but R is true.
Answer: (a) Both A and R are true, and R is the correct explanation of A.
R explains A.
Question 6 · Short answer · 1 mark
What is the index of the first character of a string?
Answer
Question 7 · Short answer · 2 marks
Why are strings called immutable?
Answer
Their characters cannot be changed after creation; an attempt such as
s[0] = 'x'raises aTypeError.Question 8 · Short answer · 3 marks
Write a program to remove all the spaces from a string.
text = input("Enter a string: ") print(text.replace(" ", ""))Output:
Enter a string: Khasi Hills and Garo Hills KhasiHillsandGaroHillsAnswer
Input (1),
replace(" ", "")(1), printing (1).Question 9 · Long answer · 5 marks
Explain the indexing of a string (positive and negative) and slicing with examples.
Answer
Sections 1.1 and 1.2.
Question 10 · Long answer · 5 marks
Explain any six string methods with examples.
Answer
Sections 1.4 and 1.5.
Study the whole chapter
15 step-by-step lessons, all 71 practice questions with hints, flashcards and mock tests for MBOSE Class XI Computer Science.