Class 11 Computer Science Chapter 8 Revision Summary Strictly NCERT

Revision Summary: Strings (NCERT Class 11 Computer Science – Chapter 8)

1. Chapter at a Glance

  • A string is a sequence of one or more UNICODE characters created by enclosing characters in single, double or triple quotes.
  • Individual characters are accessed using indexing (positive: 0 to n-1; negative: –1 to –n) and the built-in function len().
  • Strings are immutable; any attempt to modify a character after creation raises a TypeError.
  • Four string operations are supported: concatenation (+), repetition (*), membership (in, not in) and slicing (str[n:m] or str[n:m:k]).
  • A string can be traversed character-by-character using a for loop or a while loop with an index variable.
  • Python provides many built-in functions and methods (len(), upper(), lower(), find(), replace(), split(), etc.) for string manipulation.
  • User-defined functions can be written to perform tasks such as counting occurrences, replacing vowels, reversing a string and checking for palindromes.

2. Key Terms and Definitions

  • String: A sequence made up of one or more UNICODE characters (letter, digit, whitespace or any other symbol) enclosed in single, double or triple quotes.
  • Indexing: The technique of accessing an individual character of a string by writing its position inside square brackets [].
  • Positive index: Index value starting from 0 (first character) up to n–1 (last character), where n is the length of the string.
  • Negative index: Index value starting from –1 (last character) down to –n (first character).
  • Immutable: Property of a string that its contents cannot be changed after creation.
  • Concatenation: Joining two strings using the + operator.
  • Repetition: Repeating a string using the * operator.
  • Membership: Checking whether one string appears as a substring in another using in or not in.
  • Slicing: Retrieving a substring by specifying an index range str[n:m] (characters from index n inclusive to m exclusive).
  • Traversing a string: Accessing each character of the string one by one, either with a for loop or a while loop.

3. Syntax and Constructs

```python

Creating strings

str1 = 'Hello World!' str2 = "Hello World!" str3 = """Hello World!"""

Indexing and len()

ch = str1[0] # 'H' last = str1[-1] # '!' length = len(str1) # 12

Concatenation and repetition

result = str1 + str2 # 'Hello World!Hello World!' rep = str1 * 3 # 'Hello World!Hello World!Hello World!'

Membership

'W' in str1 # True 'xyz' not in str1 # True

Slicing

sub = str1[1:5] # 'ello' sub2 = str1[::2] # 'HloWrd!' rev = str1[::-1] # '!dlroW olleH'

Traversal

for ch in str1: print(ch, end='')

i = 0 while i < len(str1): print(str1[i], end='') i += 1 ```

Built-in functions/methods (general form + example)
len(str) → returns length
python len('Hello') # 5

str.upper(), str.lower(), str.title()
python 'hello'.upper() # 'HELLO'

str.find(sub[,start[,end]]), str.index(sub[,start[,end]])
python 'Hello'.find('l') # 2

str.count(sub[,start[,end]]), str.replace(old,new)
python 'Hello'.count('l') # 2 'Hello'.replace('l','*')# 'He**o'

str.startswith(sub), str.endswith(sub), str.strip(), str.split([sep]), str.join(iterable)
python 'hello'.startswith('he') # True ' a b c '.split() # ['a','b','c'] '-'.join('abc') # 'a-b-c'

4. Algorithms and Worked Logic

String traversal (for loop)
1. Start from first character.
2. Repeat until all characters are visited.
3. Print/process each character.

String traversal (while loop)
1. Initialise index = 0.
2. While index < len(str):
- Process str[index]
- index += 1

Palindrome check (two-pointer)
1. i = 0, j = len(st)–1
2. While i ≤ j:
- If st[i] != st[j] → not palindrome
- i += 1, j -= 1
3. Return True if all pairs match.

Reverse without new string (printing only)
Use range(-1, -len(st)-1, -1) and print st[i].

Replace vowels with ‘*’
Create empty new string; for each character, append ‘*’ if vowel else append original character.

5. Common Errors and Exam Pitfalls

  • Using a non-integer index (e.g., str[1.5]) → TypeError.
  • Using an out-of-range index → IndexError.
  • Trying to modify a string character (str[0] = 'a') → TypeError (immutability).
  • Forgetting that slicing str[n:m] excludes index m.
  • Confusing find() (returns –1) with index() (raises ValueError) when substring is absent.
  • Incorrect step value or negative step in slicing leading to empty or unexpected substrings.
  • Not handling empty strings in functions such as islower(), isupper(), isspace().
  • Forgetting that split() without argument splits on whitespace and removes extra spaces.
  • In palindrome or reverse logic, off-by-one errors with indices (especially len(st)-1).

A study aid reviewed by GFIS faculty — always verify with your textbook and teacher.