Home › Free notes › Class 11 Computer Science › Strings
Strings – Class 11 Computer Science notes, important questions and MCQs
NCERT Class 11 Computer Science, chapter 8. Notes, key terms, important questions with answers and MCQs.
Chapter overview
A string is a sequence of characters enclosed in single, double or triple quotes. Strings in Python are immutable, so their characters cannot be changed in place. Strings support indexing, slicing, concatenation, repetition and membership operations, can be traversed with loops, and have many built-in methods for searching, changing case, testing and splitting.
String operations
- Indexing: the first character has index 0 and the last has index −1; for s = 'PYTHON', s[0] is 'P' and s[-1] is 'N'.
- Slicing s[start:end] returns characters from start up to end − 1, for example s[1:4] is 'YTH'; a step can be given, for example s[0:6:2] is 'PTO'.
- Concatenation (+) joins strings, repetition (*) repeats them, for example 'ab' * 3 is 'ababab'; membership (in, not in) tests whether a substring exists. Assigning s[0] = 'J' gives a TypeError because strings are immutable.
Traversal and methods
- A string can be traversed with for ch in s or with a while loop using indices; len(s) gives the number of characters.
- Methods include lower(), upper(), title(), capitalize(), count(), find() (returns −1 if not found), index() (raises an error if not found), startswith(), endswith(), isalpha(), isdigit(), isalnum(), islower(), isupper(), isspace(), strip(), lstrip(), rstrip(), replace(), join(), split() and partition().
- Methods return new strings and do not modify the original, for example s.upper() returns 'PYTHON' while s is unchanged.
Key terms
- String
- an immutable sequence of characters
- Index
- the position number of a character in a string
- Slicing
- extracting a part of a string using start, end and step
- Concatenation
- joining two strings using +
- Immutable
- cannot be changed after creation
Important questions with answers
Why are strings called immutable?2 marks
Strings are immutable because their characters cannot be changed in place after the string is created. For example, s[0] = 'J' raises a TypeError. To change a string, a new string must be created, such as s = 'J' + s[1:].
Explain split() and join() with examples.3 marks
split() breaks a string into a list of substrings using a separator, by default whitespace; for example, 'red green blue'.split() returns ['red', 'green', 'blue']. join() joins elements of a sequence of strings using the string as a separator; for example, '-'.join(['a', 'b', 'c']) returns 'a-b-c'.
Write a program to count vowels, consonants, uppercase and lowercase letters in a string, and explain it.5 marks
The program reads a string and examines each character. s = input('Enter a string: '); v = c = up = low = 0; for ch in s: if ch.isalpha(): if ch.lower() in 'aeiou': v += 1, else: c += 1; and then if ch.isupper(): up += 1, elif ch.islower(): low += 1. Finally, print('Vowels:', v, 'Consonants:', c, 'Uppercase:', up, 'Lowercase:', low). Explanation: four counters are initialised to zero. The for loop traverses the string character by character. isalpha() checks whether the character is a letter, so digits, spaces and symbols are ignored. ch.lower() converts the letter to lowercase so that both 'A' and 'a' are counted as vowels when checked against 'aeiou' using the membership operator in; otherwise the letter is counted as a consonant. isupper() and islower() check the case of each letter and update the corresponding counters. For the input 'Hello World', the output is Vowels: 3, Consonants: 7, Uppercase: 2, Lowercase: 8. The program shows traversal, membership testing and string methods together.
Quick revision
- Creating strings; indexing, including negative indexing.
- Slicing with step.
- Concatenation, repetition, membership; immutability.
- Traversal and string methods.
Exam tips
- In slicing, the end index is excluded.
- find() returns −1 when not found; index() raises ValueError.
Practice MCQs
For s = 'PYTHON', s[0] is
- Y
- P
- N
- error
Show answer
Answer: (b) P. Indexing starts at 0.
The result of 'ab' * 3 is
- ab3
- aabb
- ababab
- error
Show answer
Answer: (c) ababab. * repeats the string.
The function that returns the number of characters in a string is
- count()
- size()
- len()
- length()
Show answer
Answer: (c) len(). len('hello') is 5.
For s = 'COMPUTER', s[2:5] is
- OMP
- MPU
- MPUT
- COM
Show answer
Answer: (b) MPU. Indices 2, 3 and 4.
'hello world'.title() returns
- Hello World
- Hello world
- HELLO WORLD
- hello World
Show answer
Answer: (a) Hello World. Each word starts with a capital letter.
'banana'.find('n') returns
- 1
- -1
- 3
- 2
Show answer
Answer: (d) 2. The first 'n' is at index 2.
For s = 'PYTHON', s[-3:] is
- HON
- PYT
- NOH
- THO
Show answer
Answer: (a) HON. The last three characters.
What happens on executing s = 'cat' and then s[0] = 'b'
- a TypeError occurs because strings are immutable
- s becomes 'bat'
- s becomes 'bcat'
- nothing happens
Show answer
Answer: (a) a TypeError occurs because strings are immutable. Create a new string instead.
All chapters of Class 11 Computer Science
Written by Kundali IQ from standard, well-established facts to help with revision. This is not an official NCERT publication; always read your textbook too.