Topic: Python Strings
Section A —(1 mark each)
1. Strings in Python are immutable. Explain with an example that justifies this statement.
2. What is the difference between "a" * 5 and ["a"] * 5?
3. Predict the output:
s = "PYTHON"
print(s[-4:-1])
4. What is the output of:
print("data".capitalize())
5. Which of the following will raise an error?
(a) "abc".find("z") (b) "abc".index("z") (c) both (d) none
Section B — Short Answer (2 marks each)
6. Given text = "cbse computer science", write a statement to print the last word in
uppercase without using split().
7. How does strip(), lstrip(), and rstrip() differ? Give examples.
8. What is returned by:
"Grade11CS".isalnum()
"Grade 11".isalnum()
Explain the difference in outputs.
9. Write Python code to count how many vowels appear at even indices in a string.
10. What will be the output? Justify.
s = "banana"
print(s.replace("a", "A", 2))
Section C — (4 marks each)
11. Write a function encode_vowels(s) that replaces vowels in a string s with the next
alphabet (e.g. a→b, e→f, i→j, o→p, u→v) while keeping consonants unchanged.
Example: "education" → "fdbvcatjpn"
12. Write a Python program that accepts a string from the user and:
• Prints all unique characters in sorted order.
• Displays the frequency of each character.
Example:
Input: “banana”
Output:
Unique characters: a, b, n
Frequency: a:3 b:1 n:2
13. Write a program to input a sentence and check whether it is a palindrome if all spaces
and punctuation are ignored.
Example: "Was it a car or a cat I saw?" → Palindrome
14. Write a function longest_word(sentence) that returns the longest word in the given
string.
If two or more words are of the same maximum length, return the first one.
Section D —(6 marks each)
15. The following code fragment is written by a student:
s = "The UAE promotes innovation"
print(s[4:7])
print(s[-9:])
print(s[::-3])
print(s.replace(" ", "_", 2))
(a) Predict the output for each line.
(b) Explain the slicing pattern used in line 3.
(c) Modify the last line to replace all spaces with "_".
16. Write a program to analyze a paragraph entered by the user. The program should
display:
• Total number of sentences (assume each sentence ends with ., ?, or !)
• Total number of words
• Number of uppercase words
• Word with maximum vowels
(Use appropriate string methods and loops. Ignore punctuation in counting words.)
( 5 marks)
Write a program that encrypts a string using the following rule:
• Replace every alphabet with its mirror image in the alphabet (A↔Z, B↔Y, etc.).
• Preserve the case of letters; keep non-alphabetic characters unchanged.
Example:
Input: "AbZ!" → Output: "ZaA!"