Strings — NCERT Solutions
CBSE · Class 11 · Computer Science
NCERT Solutions for Strings, CBSE Class 11 Computer Science: 7 textbook questions solved step by step. Covers Exercise and Programming Problems.
Interactive on Super Tutor
Studying Strings? Get the full interactive chapter.
Quizzes, flashcards, AI doubt-solver and a step-by-step study plan — built for NCERT solutions and more.
Free trial, no card needed.
The first 4 solutions are open to read. The other 3 are free with a Super Tutor account.
Exercise
1Consider 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())Show solution
Given: mySubject = "Computer Science"
The string has 16 characters (indices 0 to 15).
i. print(mySubject[0:len(mySubject)])
len(mySubject) = 16, so mySubject[0:16] returns the entire string.
Output:
Computer Scienceii. print(mySubject[-7:-1])
Negative index corresponds to index (character S) and corresponds to index (exclusive), so characters at indices 9 to 14 are returned: S, c, i, e, n, c.
Output:
Scienciii. print(mySubject[::2])
Slice with step 2 picks every alternate character starting from index 0:
- Indices: 0(
C), 2(m), 4(u), 6(e), 8(), 10(c), 12(e), 14(c)
Output:
Cmue ceiv. print(mySubject[len(mySubject)-1])
len(mySubject) - 1 = 15, so mySubject[15] = 'e' (last character).
Output:
e**v. print(2*mySubject)**
The * operator repeats the string 2 times.
Output:
Computer ScienceComputer Sciencevi. print(mySubject[::-2])
Slice with step starts from the last character (index 15) and moves backwards picking every alternate character:
- Indices: 15(
e), 13(n), 11(i), 9(S), 7(r), 5(t), 3(p), 1(o)
Output:
eniSrtpovii. print(mySubject[:3] + mySubject[3:])
mySubject[:3] = "Com" and mySubject[3:] = "puter Science". Concatenating them gives back the full string.
Output:
Computer Scienceviii. print(mySubject.swapcase())
swapcase() converts uppercase letters to lowercase and vice versa.
C→c,o→O,m→M,p→P,u→U,t→T,e→E,r→R, space stays,S→s,c→C,i→I,e→E,n→N,c→C,e→E
Output:
cOMPUTER sCIENCEix. print(mySubject.startswith('Comp'))
startswith() returns True if the string starts with the given prefix. "Computer Science" does start with "Comp".
(Note: The question has a typo startsworth; the correct method is startswith.)
Output:
Truex. print(mySubject.isalpha())
isalpha() returns True only if all characters are alphabetic. "Computer Science" contains a space, so it returns False.
Output:
False2Consider the following string myAddress:
myAddress = "WZ-1, New Ganga Nagar, New Delhi"
What will be the output of following string operations:
i. print(myAddress.lower())
ii. print(myAddress.upper())
iii. print(myAddress.count('New'))
iv. print(myAddress.find('New'))
v. print(myAddress.rfind('New'))
vi. print(myAddress.split(','))
vii. print(myAddress.split(' '))
viii. print(myAddress.replace('New', 'Old'))
ix. print(myAddress.partition(','))
x. print(myAddress.index('Agra'))Show solution
Given: myAddress = "WZ-1, New Ganga Nagar, New Delhi"
i. print(myAddress.lower())
lower() converts all characters to lowercase.
Output:
wz-1, new ganga nagar, new delhiii. print(myAddress.upper())
upper() converts all characters to uppercase.
Output:
WZ-1, NEW GANGA NAGAR, NEW DELHIiii. print(myAddress.count('New'))
count() returns the number of non-overlapping occurrences of the substring 'New' in the string. 'New' appears at positions corresponding to "New Ganga" and "New Delhi" — twice.
Output:
2iv. print(myAddress.find('New'))
find() returns the index of the first occurrence of the substring.
Counting characters in "WZ-1, New Ganga Nagar, New Delhi":
W=0,Z=1,-=2,1=3,,=4,=5,N=6
First 'New' starts at index 6.
Output:
6v. print(myAddress.rfind('New'))
rfind() returns the index of the last occurrence of the substring.
Counting: "WZ-1, New Ganga Nagar, " has 23 characters (indices 0–22), so the second 'New' starts at index 23.
Output:
23vi. print(myAddress.split(','))
split(',') splits the string at every comma and returns a list.
Output:
['WZ-1', ' New Ganga Nagar', ' New Delhi']vii. print(myAddress.split(' '))
split(' ') splits the string at every single space and returns a list.
Output:
['WZ-1,', 'New', 'Ganga', 'Nagar,', 'New', 'Delhi']viii. print(myAddress.replace('New', 'Old'))
replace('New', 'Old') replaces all occurrences of 'New' with 'Old'.
Output:
WZ-1, Old Ganga Nagar, Old Delhiix. print(myAddress.partition(','))
partition(',') splits the string at the first occurrence of ',' and returns a tuple of three parts: (before separator, separator, after separator).
Output:
('WZ-1', ',', ' New Ganga Nagar, New Delhi')x. print(myAddress.index('Agra'))
index() raises a ValueError if the substring is not found. 'Agra' does not exist in myAddress.
Output:
ValueError: substring not foundProgramming Problems
1Write 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).Show solution
Concept Used:
input()to read lines until an empty line (Enter) is pressed.- String methods
isalpha(),isdigit()to classify characters. - A character that is neither alphabetic nor a digit nor a space is a special symbol.
- Words are counted by splitting on spaces.
Program:
total_chars = 0
total_alpha = 0
total_digits = 0
total_special = 0
total_words = 0
print("Enter lines of text (press Enter on empty line to stop):")
while True:
line = input()
if line == "": # Stop when user presses Enter on empty line
break
total_chars += len(line)
for ch in line:
if ch.isalpha():
total_alpha += 1
elif ch.isdigit():
total_digits += 1
elif ch == ' ':
pass # spaces counted in total_chars but not special
else:
total_special += 1
# Count words (each word separated by one space)
words = line.split(' ')
# Remove empty strings caused by leading/trailing spaces
words = [w for w in words if w != '']
total_words += len(words)
print("Total characters (including spaces):", total_chars)
print("Total alphabets :", total_alpha)
print("Total digits :", total_digits)
print("Total special symbols :", total_special)
print("Total words :", total_words)Sample Run:
Enter lines of text (press Enter on empty line to stop):
Hello World 123!
Total characters (including spaces): 16
Total alphabets : 10
Total digits : 3
Total special symbols : 1
Total words : 32Write 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)Show solution
Concept Used:
- Split the string into individual words using
split(). - Capitalise the first letter of each word using
capitalize()(or manually using indexing). - Join the words back using
join().
Program:
def toTitleCase(sentence):
"""
Converts a string to title case.
Parameter: sentence (str) - input string
Returns : title-cased string
"""
words = sentence.split() # Split on whitespace
title_words = []
for word in words:
# Capitalise first letter, lowercase the rest
title_word = word[0].upper() + word[1:].lower()
title_words.append(title_word)
return ' '.join(title_words)
# --- Driver code ---
text = input("Enter a string: ")
result = toTitleCase(text)
print("Title Case String:", result)Sample Run:
Enter a string: hello world from python
Title Case String: Hello World From PythonNote: Python also provides a built-in str.title() method, but the above function demonstrates the logic manually as expected in a board exam.
Free with a Super Tutor account
Free with a Super Tutor account
Free with a Super Tutor account
3 more solved questions in Strings
They are free with a Super Tutor account, along with practice quizzes and flashcards for this chapter. Free to start, no card needed.
Frequently Asked Questions
What are the important topics in Strings for CBSE Class 11 Computer Science?
Are these NCERT Solutions for Strings free?
How should I revise Strings for Class 11 exams?
Sources & Official References
- NCERT Official — ncert.nic.in
- CBSE Academic — cbseacademic.nic.in
- CBSE Official — cbse.gov.in
- National Education Policy 2020 — education.gov.in
Content is aligned to the official syllabus. Refer to the board website for the latest curriculum.
More resources for Strings
Practice Quiz
Test yourself with a quick quiz
Important Questions
Exam-style questions with answers
Revision Notes
Key points for last-minute revision
Formula Sheet
The chapter's formulas in one place
Chapter Summary
Understand the chapter at a glance
Concept Maps
See how topics connect
Study Plan
Step-by-step plan for this chapter
Flashcards
Quick-fire cards for active recall
Syllabus
What topics to cover
For serious students
Get the full Strings chapter — start free.
Quizzes, flashcards, an AI doubt solver and a study plan for CBSE Class 11 Computer Science. Free to start, no card needed.