Revision Summary: Strings (NCERT Class 11 Computer Science – Chapter 8)
len().+), repetition (*), membership (in, not in) and slicing (str[n:m] or str[n:m:k]).for loop or a while loop with an index variable.len(), upper(), lower(), find(), replace(), split(), etc.) for string manipulation.[].+ operator.* operator.in or not in.str[n:m] (characters from index n inclusive to m exclusive).for loop or a while loop.```python
str1 = 'Hello World!' str2 = "Hello World!" str3 = """Hello World!"""
ch = str1[0] # 'H' last = str1[-1] # '!' length = len(str1) # 12
result = str1 + str2 # 'Hello World!Hello World!' rep = str1 * 3 # 'Hello World!Hello World!Hello World!'
'W' in str1 # True 'xyz' not in str1 # True
sub = str1[1:5] # 'ello' sub2 = str1[::2] # 'HloWrd!' rev = str1[::-1] # '!dlroW olleH'
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'
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.
str[1.5]) → TypeError.str[0] = 'a') → TypeError (immutability).str[n:m] excludes index m.find() (returns –1) with index() (raises ValueError) when substring is absent.islower(), isupper(), isspace().split() without argument splits on whitespace and removes extra spaces.len(st)-1).A study aid reviewed by GFIS faculty — always verify with your textbook and teacher.