NCERT Solutions for Class 11 Science Computer science Chapter 8 Strings are provided here with simple step-by-step explanations. These solutions for Strings are extremely popular among Class 11 Science students for Computer science Strings Solutions come handy for quickly completing your homework and preparing for exams. All questions and answers from the NCERT Book of Class 11 Science Computer science Chapter 8 are provided here for you for free. You will also love the ad-free experience on Meritnation’s NCERT Solutions. All NCERT Solutions for class Class 11 Science Computer science are prepared by experts and are 100% accurate.

Page No 187:

Question 1:

Consider the following string mySubject: 
                    mySubject = "Computer Science"
What will be the output of the following string operations:- 

  1. print(mySubject[0:len(mySubject)])
  2. print(mySubject[-7:-1])
  3. print(mySubject[::2])
  4. print(mySubject[len(mySubject)-1])
  5. print(2*mySubject)
  6. print(mySubject[::-2])
  7. print(mySubject[:3] + mySubject[3:])
  8. print(mySubject.swapcase())
  9. print(mySubject.startswith('Comp'))
  10. print(mySubject.isalpha())

Answer:

The output of the given string operations are as follows:

  1. Computer Science
  2. Scienc
  3. Cmue cec
  4. e
  5. Computer ScienceComputer Science
  6. eniSrtpo
  7. Computer Science
  8. cOMPUTER sCIENCE
  9. True
  10. False

Page No 187:

Question 2:

Consider the following string myAddress:
            myAddress = "WZ-1,New Ganga Nagar,New Delhi"
What will be the output of following string operations :

  1. print(myAddress.lower())
  2. print(myAddress.upper())
  3. print(myAddress.count('New'))
  4. print(myAddress.find('New'))
  5. print(myAddress.rfind('New'))
  6. print(myAddress.split(','))
  7. print(myAddress.split(' '))
  8. print(myAddress.replace('New','Old'))
  9. print(myAddress.partition(','))
  10. print(myAddress.index('Agra'))

Answer:

The output of the given string operations are as follows:

  1. wz-1,new ganga nagar,new delhi
  2. WZ-1,NEW GANGA NAGAR,NEW DELHI
  3. 2
  4. 5
  5. 21
  6. ['WZ-1', 'New Ganga Nagar', 'New Delhi']
  7. ['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']
  8. WZ-1,Old Ganga Nagar,Old Delhi
  9. ('WZ-1', ',', 'New Ganga Nagar,New Delhi')
  10. Error .. Substring Not found



Page No 188:

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

Answer:

Program:
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 

Page No 188:

Question 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)

Answer:

Program:
#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!

Page No 188:

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

Answer:

Program 1:
#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!

​​

Page No 188:

Question 4:

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

Answer:

Program:
#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

Page No 188:

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

Answer:


Program:
#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.



View NCERT Solutions for all chapters of Class 11