Exercise Solutions Available
Step-by-step answers for all textbook questions in this chapter
Strings and String Manipulations — TN Class 11 CS Exercise Solutions
Complete exercise solutions for the Tamil Nadu Board Class 11 Computer Science chapter Strings and String Manipulations. Covers string creation, indexing, slicing, operations, methods, and formatting with working Python code.
How to Use This Page
1. Attempt each exercise on your own first — write Python code before checking solutions. 2. Run every example in IDLE or a Python editor to verify the output. 3. Practise slicing — draw the index positions on paper before solving slicing questions. 4. Understand immutability — remember that strings cannot be changed in place; methods return new strings. 5. Memorise string methods — many MCQ and fill-in-the-blank questions test method names and return types.
Key Concepts (Quick Recap)
| Concept | Syntax/Example | Details |
|---|---|---|
| String creation | s = "Hello" | Sequence of characters in quotes |
| Single char | s = 'A' | A string of length 1 |
| Triple quotes | s = """Line 1\nLine 2""" | Multi-line strings |
| Indexing | s[0], s[-1] | Access individual characters |
| Slicing | s[start:end:step] | Extract substring (end is exclusive) |
| Concatenation | s1 + s2 | Join two strings |
| Repetition | s * 3 | Repeat string n times |
| Membership | 'el' in 'Hello' | Check if substring exists → True |
| Length | len(s) | Number of characters |
| Immutability | Strings cannot be changed | Methods return NEW strings |
String Indexing Diagram for s = "PYTHON":
| Expression | Result | Explanation |
|---|---|---|
s[0] | 'P' | First character |
s[5] | 'N' | Last character (positive index) |
s[-1] | 'N' | Last character (negative index) |
s[-6] | 'P' | First character (negative index) |
s[1:4] | 'YTH' | From index 1 up to (not including) 4 |
s[:3] | 'PYT' | From beginning up to index 3 |
s[3:] | 'HON' | From index 3 to end |
s[::-1] | 'NOHTYP' | Reverse the string |
s[0:6:2] | 'PTO' | Every 2nd character |
Common String Methods:
| Method | Example | Result | Description |
|---|---|---|---|
upper() | "hi".upper() | "HI" | Convert to uppercase |
lower() | "HI".lower() | "hi" | Convert to lowercase |
title() | "hello world".title() | "Hello World" | First letter of each word uppercase |
capitalize() | "hello".capitalize() | "Hello" | First letter uppercase |
strip() | " hi ".strip() | "hi" | Remove leading/trailing spaces |
lstrip() | " hi ".lstrip() | "hi " | Remove leading spaces |
rstrip() | " hi ".rstrip() | " hi" | Remove trailing spaces |
find() | "hello".find("ll") | 2 | Index of first occurrence, -1 if not found |
count() | "banana".count("a") | 3 | Count occurrences |
replace() | "hi".replace("h", "H") | "Hi" | Replace substring |
split() | "a,b,c".split(",") | ["a","b","c"] | Split into list |
join() | ",".join(["a","b","c"]) | "a,b,c" | Join list into string |
startswith() | "hello".startswith("he") | True | Check if string starts with prefix |
endswith() | "hello".endswith("lo") | True | Check if string ends with suffix |
isalpha() | "abc".isalpha() | True | Check if all characters are alphabetic |
isdigit() | "123".isdigit() | True | Check if all characters are digits |
isalnum() | "abc123".isalnum() | True | Check if all characters are alphanumeric |
swapcase() | "HeLLo".swapcase() | "hEllO" | Swap case of each character |
center() | "hi".center(10) | " hi " | Center string in given width |
zfill() | "5".zfill(3) | "005" | Pad with zeros on the left |
I. Choose the Best Answer (MCQs)
Q1. Strings in Python are: (a) Mutable (b) Immutable (c) Both (d) None of these
Strings in Python cannot be modified after creation. Any operation that appears to modify a string actually creates a new string.
Q2. What is the output of "Hello"[1:4]? (a) "Hell" (b) "ello" (c) "ell" (d) "Hel"
Slicing [1:4] starts at index 1 ('e') and goes up to (but not including) index 4 ('o'). Result: "ell".
Q3. Which method converts a string to uppercase? (a) upper() (b) uppercase() (c) toupper() (d) toUpper()
The upper() method returns a new string with all characters converted to uppercase. "hello".upper() gives "HELLO".
Q4. What does "Python".find("th") return? (a) 2 (b) 0 (c) -1 (d) Error
find() returns the index of the first occurrence of the substring. In "Python", "th" starts at index 2 (P=0, y=1, t=2, h=3). Note: Python is case-sensitive; "th" matches because find searches for lowercase "th" which appears at positions 2-3.
Wait — "Python" has 'P'(0), 'y'(1), 't'(2), 'h'(3), 'o'(4), 'n'(5). So "th" is at indices 2-3. find("th") returns 2.
*Q5. What is the output of "abc" 3?** (a) "abc3" (b) "abcabcabc" (c) Error (d) "abc abc abc"
The operator with a string and an integer repeats the string. "abc" 3 produces "abcabcabc".
Q6. The len("Hello World") returns: (a) 10 (b) 11 (c) 12 (d) Error
len() counts all characters including the space. "Hello World" has 11 characters: H-e-l-l-o-[space]-W-o-r-l-d.
Q7. Which method removes leading and trailing whitespace? (a) trim() (b) strip() (c) remove() (d) clean()
strip() removes leading and trailing whitespace. lstrip() removes only leading, and rstrip() removes only trailing whitespace.
II. Fill in the Blanks
1. Strings in Python are immutable — they cannot be changed after creation. 2. The len() function returns the number of characters in a string. 3. The index of the first character in a string is 0. 4. The index of the last character in a string of length n is -1 (or n-1). 5. The slicing syntax is string[start:end:step]. 6. "Hello" + "World" results in "HelloWorld" (concatenation without space). 7. "Ha" * 3 results in "HaHaHa" (string repetition). 8. The upper() method converts a string to uppercase. 9. The lower() method converts a string to lowercase. 10. The split() method splits a string into a list based on a delimiter. 11. The join() method joins elements of a list into a string using a separator. 12. "Hello".find("lo") returns 3 (index of 'l' where "lo" starts). 13. "banana".count("a") returns 3. 14. To reverse a string, we use the slicing string[::-1]. 15. The strip() method removes leading and trailing whitespace from a string.
III. True or False (with corrections for false statements)
replace() method modifies the original string."5" + "3" returns 8.split() method returns a string.IV. Match the Following
| Column A | Column B |
|---|---|
1. upper() | (e) Convert to uppercase |
2. lower() | (a) Convert to lowercase |
3. split() | (d) Split string into list |
4. join() | (b) Join list into string |
5. find() | (f) Return index of substring |
6. count() | (c) Count occurrences of substring |
| Column A | Column B |
|---|---|
1. "Hello"[0] | (c) 'H' |
2. "Hello"[-1] | (f) 'o' |
3. "Hello"[1:4] | (a) 'ell' |
4. "Hello"[::-1] | (e) 'olleH' |
5. len("Hello") | (b) 5 |
6. "Hello" + "World" | (d) 'HelloWorld' |
V. Answer Briefly (2-Mark Questions)
find() and index() methods?VI. Answer in Detail (5-Mark Questions)
s = "Hello World"
print(len(s)) # 11 (including the space)
s = "hello WORLD"
print(s.upper()) # "HELLO WORLD" — all uppercase
print(s.lower()) # "hello world" — all lowercase
print(s.title()) # "Hello World" — first letter of each word uppercase
print(s.capitalize()) # "Hello world" — first letter uppercase
print(s.swapcase()) # "HELLO world" — swap case of each character
s = "banana"
print(s.find("na")) # 2 — index of first occurrence
print(s.find("xyz")) # -1 — not found
print(s.rfind("na")) # 4 — index from the right
print(s.index("na")) # 2 — same as find but raises error if not found
print(s.count("a")) # 3 — number of occurrences
print(s.count("na")) # 2
print(s.startswith("ban")) # True
print(s.endswith("na")) # True
s = " Hello World "
print(s.strip()) # "Hello World" — remove leading/trailing spaces
print(s.lstrip()) # "Hello World " — remove leading spaces
print(s.rstrip()) # " Hello World" — remove trailing spaces
text = "I like cats"
print(text.replace("cats", "dogs")) # "I like dogs"
print(text.replace("i", "I")) # "I lIke cats" — replaces all occurrences
# Split a string into a list
s = "one,two,three"
print(s.split(",")) # ['one', 'two', 'three']
text = "Hello World"
print(text.split()) # ['Hello', 'World'] — default splits on whitespace
# Join a list into a string
words = ["Python", "is", "fun"]
print(" ".join(words)) # "Python is fun"
print("-".join(words)) # "Python-is-fun"
print("".join(words)) # "Pythonisfun"
print("hello".isalpha()) # True — all alphabetic
print("12345".isdigit()) # True — all digits
print("hello123".isalnum()) # True — alphabetic or digits
print(" ".isspace()) # True — all whitespace
print("Hello".isupper()) # False — not all uppercase
print("HELLO".isupper()) # True — all uppercase
print("hello".islower()) # True — all lowercase
# center(), ljust(), rjust()
s = "Hi"
print(s.center(10)) # " Hi " — center in 10-character width
print(s.ljust(10)) # "Hi " — left justify
print(s.rjust(10)) # " Hi" — right justify
# zfill()
print("42".zfill(5)) # "00042" — pad with zeros on the left
Common Mistakes to Avoid
1. Treating strings as mutable — You cannot do s[0] = "H". Strings are immutable. Use s = "H" + s[1:] or s = s.replace(...).
2. Confusing + for numbers vs strings — "5" + "3" is "53" (concatenation), not 8. Use int("5") + int("3") for arithmetic.
3. Off-by-one in slicing — s[1:4] includes index 1 but excludes index 4. It gives characters at indices 1, 2, 3.
4. Case sensitivity in searches — "Hello".find("hello") returns -1 because "Hello" ≠ "hello". Use .lower() first: "Hello".lower().find("hello").
5. Forgetting that string methods return new strings — s.upper() does NOT modify s. It returns a new string. Write s = s.upper() to update the variable.
6. Using index() without error handling — find() returns -1 if not found; index() raises ValueError. Use find() for safe searches.
7. Confusing split() output — split() returns a list, not a string. "a,b,c".split(",") gives ["a", "b", "c"].
Exam Checklist
Before your exam, make sure you can:
- [ ] Create strings using single, double, and triple quotes
- [ ] Use positive and negative indexing to access characters
- [ ] Slice strings with start, end, and step parameters
- [ ] Reverse a string using
[::-1] - [ ] Concatenate and repeat strings with
+and* - [ ] Use membership operators (
in,not in) with strings - [ ] Apply case conversion methods (upper, lower, title, capitalize, swapcase)
- [ ] Use search methods (find, rfind, count, startswith, endswith)
- [ ] Strip whitespace with strip(), lstrip(), rstrip()
- [ ] Replace substrings with replace()
- [ ] Split and join strings (split(), join())
- [ ] Check string properties (isalpha, isdigit, isalnum, isspace)
- [ ] Write programs: palindrome check, vowel/consonant count, word reversal, anagram check
- [ ] Explain why strings are immutable with examples
