Kundali IQ

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

  1. For s = 'PYTHON', s[0] is

    1. Y
    2. P
    3. N
    4. error
    Show answer

    Answer: (b) P. Indexing starts at 0.

  2. The result of 'ab' * 3 is

    1. ab3
    2. aabb
    3. ababab
    4. error
    Show answer

    Answer: (c) ababab. * repeats the string.

  3. The function that returns the number of characters in a string is

    1. count()
    2. size()
    3. len()
    4. length()
    Show answer

    Answer: (c) len(). len('hello') is 5.

  4. For s = 'COMPUTER', s[2:5] is

    1. OMP
    2. MPU
    3. MPUT
    4. COM
    Show answer

    Answer: (b) MPU. Indices 2, 3 and 4.

  5. 'hello world'.title() returns

    1. Hello World
    2. Hello world
    3. HELLO WORLD
    4. hello World
    Show answer

    Answer: (a) Hello World. Each word starts with a capital letter.

  6. 'banana'.find('n') returns

    1. 1
    2. -1
    3. 3
    4. 2
    Show answer

    Answer: (d) 2. The first 'n' is at index 2.

  7. For s = 'PYTHON', s[-3:] is

    1. HON
    2. PYT
    3. NOH
    4. THO
    Show answer

    Answer: (a) HON. The last three characters.

  8. What happens on executing s = 'cat' and then s[0] = 'b'

    1. a TypeError occurs because strings are immutable
    2. s becomes 'bat'
    3. s becomes 'bcat'
    4. 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.