Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+2 votes
3.0k views
in Computer by (43.0k points)
closed by

NCERT Solutions Class 11, Computer Science, Chapter- 8, Strings

To thoroughly grasp this chapter and excel in CBSE exams and competitive tests, utilizing NCERT Solutions is highly recommended. These solutions, crafted by experts in the field, delve into all key concepts covered in the chapter. Specifically designed for the CBSE curriculum, they ensure a comprehensive understanding and invaluable support in your academic endeavors.

In these NCERT Solutions for Class 11 Computer Science, we have discussed all types of NCERT intext questions and exercise questions.

Concepts covered in Class 11 Computer Science chapter- 8 Strings, are :

  • Introduction to Strings
  • String Operations
  • Traversing a String
  • Strings Methods and Built-in Functions
  • Handling Strings

Our NCERT Solutions for Class 11 Computer Science provide detailed explanations to assist students with their homework and assignments. Proper command and ample practice of topic-related questions provided by our NCERT solutions is the most effective way to achieve full marks in your exams. Begin studying right away to ace your exams.

Easily access all solutions and practice questions at your fingertips to kick-start your preparation immediately.

2 Answers

+2 votes
by (43.0k points)
selected by
 
Best answer

NCERT Solutions Class 11, Computer Science, Chapter- 8, Strings

Exercise

1. Consider the following string mySubject:

mySubject = "Computer Science"

What will be the output of the following string operations :

i. print(mySubject[0:len(mySubject)])

Solution:

Computer Science

ii. print(mySubject[-7:-1])

Solution:

Scienc

iii. print(mySubject[::2])

Solution:

Cmue cec

iv. print(mySubject[len(mySubject)-1])

Solution:

e

v. print(2*mySubject)

Solution:

Computer ScienceComputer Science

vi. print(mySubject[::-2])

Solution:

eniSrtpo

vii. print(mySubject[:3] + mySubject[3:])

Solution:

Computer Science

viii. print(mySubject.swapcase())

Solution:

cOMPUTER sCIENCE

ix. print(mySubject.startswith('Comp'))

Solution:

True

x. print(mySubject.isalpha())

Solution:

False

2. Consider 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())

Solution:

wz-1, new ganga nagar, new delhi

ii. print(myAddress.upper())

Solution:

WZ-1, NEW GANGA NAGAR, NEW DELHI

iii. print(myAddress.count('New'))

Solution:

2

iv. print(myAddress.find('New'))

Solution:

5

v. print(myAddress.rfind('New'))

Solution:

21

vi. print(myAddress.split(','))

Solution:

['WZ-1', 'New Ganga Nagar', 'New Delhi']

vii. print(myAddress.split(' '))

Solution:

['WZ-1, New', 'Ganga', 'Nagar,New', 'Delhi']

viii. print(myAddress.replace('New','Old'))

Solution:

WZ-1, Old Ganga Nagar, Old Delhi

ix. print(myAddress.partition(','))

Solution:

('WZ-1' , ', ', 'New Ganga Nagar, New Delhi')

x. print(myAddress.index('Agra'))

Solution:

Error .. Substring Not found

Programming Problem

1. 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).

Solution:

userInput = input("Write a sentence: ")
#Count the total number of characters in the text (including white spaces)which is the length of string
totalChar = len(userInput)
print("Total Characters: ",totalChar)
#Count the total number of alphabets,digit and special characters by looping through each character and incrementing the respective variable value
totalAlpha = totalDigit = totalSpecial = 0
for a in userInput:
    if a.isalpha():
        totalAlpha += 1
    elif a.isdigit():
        totalDigit += 1
    else:
        totalSpecial += 1

print("Total Alphabets: ",totalAlpha)
print("Total Digits: ",totalDigit)
print("Total Special Characters: ",totalSpecial)

#Count number of words (Assume that each word is separated by one space)
#Therefore, 1 space:2 words, 2 space:3 words and so on
totalSpace = 0
for b in userInput:
    if b.isspace():
        totalSpace += 1

print("Total Words in the Input :",(totalSpace + 1))

OUTPUT:
Write a sentence: Good Morning!
Total Characters: 13
Total Alphabets: 11
Total Digits: 0
Total Special Characters: 2
Total Words in the Input: 2 

2. 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)

Solution:

#Changing a string to title case using title() function
def convertToTitle(string):
    titleString = string.title();
    print("The input string in title case is:",titleString)        
    
userInput = input("Write a sentence: ")
#Counting the number of space to get the number of words
totalSpace = 0
for b in userInput:
    if b.isspace():
        totalSpace += 1
#If the string is already in title case
if(userInput.istitle()):
    print("The String is already in title case")
#If the string is not in title case and consists of more than one word
elif(totalSpace > 0):
    convertToTitle(userInput)
#If the string is of one word only
else:
    print("The String is of one word only")

OUTPUT:
Write a sentence: good evening!
The input string in title case is: Good Evening!

3. 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.

Solution:

#deleteChar function to delete all occurrences of char from string using replace() function
def deleteChar(string,char):
    #each char is replaced by empty character
    newString = string.replace(char,'')
    return newString

userInput = input("Enter any string: ")
delete = input("Input the character to delete from the string: ")

newStr = deleteChar(userInput,delete)

print("The new string after deleting all occurrences of",delete,"is: ",newStr)

OUTPUT:
Enter any string: Good Morning!
Input the character to delete from the string: o
The new string after deleting all occurrences of o is: Gd Mrning!


Program 2:

#deleteChar function to delete all occurrences of char from string without using replace() function
def deleteChar(string,char):
    newString = ''
    #Looping through each character of string
    for a in string:
        #if character matches, replace it by empty string
        if a == char:
            #continue
            newString += ''
        else:
            newString += a
    return newString

userInput = input("Enter any string: ")
delete = input("Input the character to delete from the string: ")

newStr = deleteChar(userInput,delete)

print("The new string after deleting all occurrences of",delete,"is: ",newStr)

OUTPUT:
Enter any string: Good Evening!
Input the character to delete from the string: o
The new string after deleting all occurrences of o is: Gd Evening!
+2 votes
by (43.0k points)

4. Input a string having some digits. Write a function to return the sum of digits present in this string.

Solution:

#sumDigit function to sum all the digits
def sumDigit(string):
    sum = 0
    for a in string:
    #Checking if a character is digit and adding it
        if(a.isdigit()):
            sum += int(a)
    return sum

userInput = input("Enter any string with digits: ")

#Calling the sumDigit function
result = sumDigit(userInput)

#Printing the sum of all digits
print("The sum of all digits in the string '",userInput,"' is:",result)

OUTPUT:
Enter any string with digits: The cost of this table is Rs. 3450
The sum of all digits in the string ' The cost of this table is Rs. 3450 ' is: 12

5. 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.

Solution:

#replaceChar function to replace space with hyphen
def replaceChar(string):
    return string.replace(' ','-')

userInput = input("Enter a sentence: ")

#Calling the replaceChar function to replace space with hyphen
result = replaceChar(userInput)

#Printing the modified sentence
print("The new sentence is:",result)

OUTPUT:
Enter a sentence: Python has several built-in functions that allow us to work with strings.
The new sentence is: Python-has-several-built-in-functions-that-allow-us-to-work-with-strings.


The same task can also be accomplished without using the string.replace() function. The program for the same can be written as :

Program:

#replaceChar function to replace space with hyphen
def replaceChar(string):
    newString = ''
    #Looping through each character of string
    for a in string:
        #if char is space, replace it with hyphen
        if a == ' ':
            newString += '-'
        #else leave the character as it is
        else:
            newString += a
    return newString

userInput = input("Enter a sentence: ")

#Calling the replaceChar function to replace space with hyphen
result = replaceChar(userInput)

#Printing the modified sentence
print("The new sentence is:",result)

OUTPUT:
Enter a sentence: Python has several built-in functions that allow us to work with strings.
The new sentence is: Python-has-several-built-in-functions-that-allow-us-to-work-with-strings.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...