Class | Subject | Book | Chapter | Session | Questions covered |
|---|---|---|---|---|---|
XI | Computer Science | Computer Science (NCERT) | 8 – Strings | 2026-27 | 7 of 7 |
Chapter at a glance
Idea | Key point |
|---|---|
String | Sequence of characters in quotes; immutable |
Indexing | Positive from 0 (left), negative from −1 (right) |
Slicing | s[start:stop:step] — stop excluded; s[::-1] reverses |
Operators | + concatenation, * repetition, in / not in membership |
Traversal | for ch in s: or for i in range(len(s)): |
Methods | len(), lower(), upper(), title(), swapcase(), count(), find(), rfind(), index(), startswith(), isalpha(), isdigit(), split(), partition(), replace(), strip(), join() |
find vs index | find() returns −1 if not found; index() raises ValueError |
Exercise Questions
Consider the following string mySubject: mySubject = "Computer Science". What will be the output of the following string operations:
i. print(mySubject[0:len(mySubject)]) ii. print(mySubject[-7:-1]) iii. print(mySubject[::2]) iv. print(mySubject[len(mySubject)-1]) v. print(2*mySubject) vi. print(mySubject[::-2]) vii. print(mySubject[:3] + mySubject[3:]) viii. print(mySubject.swapcase()) ix. print(mySubject.startswith('Comp')) x. print(mySubject.isalpha())
Answer
Answer
The string has 16 characters (index 0–15); index 8 is the space.
Part | Output | Reason |
|---|---|---|
i | Computer Science | Slice 0 to 16 = the whole string |
ii | Scienc | Indices −7 to −2 (‘S’ … ‘c’); −1 (‘e’) excluded |
iii | Cmue cec | Every second character from index 0 |
iv | e | Last character (index 15) |
v | Computer ScienceComputer Science | Repetition |
vi | eniSrtpo | Every second character from the end, backwards |
vii | Computer Science | The two slices join back into the original |
viii | cOMPUTER sCIENCE | Case of each letter is swapped |
ix | True | String begins with ‘Comp’ |
x | False | The space is not an alphabet |
- Computer Science ii) Scienc iii) Cmue cec iv) e v) Computer ScienceComputer Science vi) eniSrtpo vii) Computer Science viii) cOMPUTER sCIENCE ix) True x) False
Consider the following string myAddress: myAddress = "WZ-1,New Ganga Nagar,New Delhi". What will be the output of following string operations:
i. lower() ii. upper() iii. count('New') iv. find('New') v. rfind('New') vi. split(',') vii. split(' ') viii. replace('New','Old') ix. partition(',') x. index('Agra')
Answer
Answer
Part | Output |
|---|---|
i | wz-1,new ganga nagar,new delhi |
ii | WZ-1,NEW GANGA NAGAR,NEW DELHI |
iii | 2 |
iv | 5 (first ‘New’ starts at index 5) |
v | 21 (last ‘New’ starts at index 21) |
vi | [‘WZ-1’, ‘New Ganga Nagar’, ‘New Delhi’] |
vii | [‘WZ-1,New’, ‘Ganga’, ‘Nagar,New’, ‘Delhi’] |
viii | WZ-1,Old Ganga Nagar,Old Delhi |
ix | (‘WZ-1’, ‘,’, ‘New Ganga Nagar,New Delhi’) — split at the first comma only |
x | Error — ValueError: substring not found (‘Agra’ is not in the string) |
- wz-1,new ganga nagar,new delhi ii) WZ-1,NEW GANGA NAGAR,NEW DELHI iii) 2 iv) 5 v) 21 vi) [‘WZ-1’, ‘New Ganga Nagar’, ‘New Delhi’] vii) [‘WZ-1,New’, ‘Ganga’, ‘Nagar,New’, ‘Delhi’] viii) WZ-1,Old Ganga Nagar,Old Delhi ix) (‘WZ-1’, ‘,’, ‘New Ganga Nagar,New Delhi’) x) ValueError
Question P1
Write a program to input line(s) of text from the user until enter is pressed. Count the total number of characters in the text (including white spaces), total number of alphabets, total number of digits, total number of special symbols and total number of words in the given text. (Assume that each word is separated by one space).
Answer
text = ""
while True:
line = input()
if line == "": # Enter pressed on an empty line
break
text += line + " "
text = text.strip()
alphabets = digits = special = 0
for ch in text:
if ch.isalpha():
alphabets += 1
elif ch.isdigit():
digits += 1
elif ch != " ":
special += 1
words = len(text.split()) if text else 0
print("Total characters:", len(text))
print("Alphabets:", alphabets)
print("Digits:", digits)
print("Special symbols:", special)
print("Words:", words)
Sample input: Hello World 2026! → characters 17, alphabets 10, digits 4, special symbols 1, words 3.
Read lines until an empty line, then traverse the text using isalpha(), isdigit() and a non-space check for symbols; words = len(text.split()).
Question P2
Write a user defined function to convert a string with more than one word into title case string where string is passed as parameter. (Title case means that the first letter of each word is capitalised)
Answer
def to_title(s):
words = s.split(" ")
result = []
for w in words:
if w:
result.append(w[0].upper() + w[1:].lower())
else:
result.append(w)
return " ".join(result)
st = input("Enter a sentence: ")
print(to_title(st))
Sample run: circle of learning → Circle Of Learning (the built-in st.title() gives the same result).
Split into words, capitalise the first letter of each (w[0].upper() + w[1:].lower()) and join with spaces.
Question P3
Write a function deleteChar() which takes two parameters one is a string and other is a character. The function should create a new string after deleting all occurrences of the character from the string and return the new string.
Answer
def deleteChar(s, ch):
new = ""
for c in s:
if c != ch:
new += c
return new
st = input("Enter a string: ")
c = input("Enter character to delete: ")
print(deleteChar(st, c))
Sample run: banana, a → bnn
Traverse the string and build a new string from every character not equal to ch; return it.
Question P4
Input a string having some digits. Write a function to return the sum of digits present in this string.
Answer
def sum_of_digits(s):
total = 0
for ch in s:
if ch.isdigit():
total += int(ch)
return total
st = input("Enter a string: ")
print("Sum of digits =", sum_of_digits(st))
Sample run: abc12x9 → 1 + 2 + 9 = 12
Add int(ch) for every character for which ch.isdigit() is True (abc12x9 → 12).
Question P5
Write a function that takes a sentence as an input parameter where each word in the sentence is separated by a space. The function should replace each blank with a hyphen and then return the modified sentence.
Answer
def hyphenate(sentence):
new = ""
for ch in sentence:
if ch == " ":
new += "-"
else:
new += ch
return new
s = input("Enter a sentence: ")
print(hyphenate(s))
Sample run: I love Python → I-love-Python (equivalent to sentence.replace(" ", "-")).
Traverse the sentence, adding “-” for each space and the character otherwise; return the new string.
Topic checklist
- ☒ Indexing, slicing, repetition (Q1)
- ☒ String methods (Q1, 2)
- ☒ Traversal and character tests (P1, P3, P4)
- ☒ String-processing functions (P2–P5)