Humanities/Arts Exam  >  Humanities/Arts Notes  >  Computer Science for Class 11  >  NCERT Solutions: Strings

Strings NCERT Solutions | Computer Science for Class 11 - Humanities/Arts PDF Download

Exercise 

Q1: Consider the following string mySubject:
mySubject = “Computer Science”
What will be the output of the following string operations :

(i) print(mySubject[0 : len(mySubject)])
Ans: Computer Science

(ii) print(mySubject[-7 : -1])
Ans: Scienc

(iiiprint(mySubject[: : 2])
Ans: Cmue cec

(iv) print(mySubject[len(mySubject)-1])
Ans: e

(v) print(2 * mySubject)
Ans: Computer ScienceComputer Science

(vi) print(mySubject[: : -2])
Ans: eniSrtpo

(vii) print(mySubject[ : 3] + mySubject[3 : ])
Ans: Computer Science

(viii) print(mySubject.swapcase())
Ans: cOMPUTER sCIENCE

(ix) print(mySubject.startswith(‘Comp’))
Ans: True

(x) print(mySubject.isalpha())
Ans: False

Q2: 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())
Ans:
wz-1,new ganga nagar,new delhi

(ii) print(myAddress.upper())
Ans:
WZ-1,NEW GANGA NAGAR,NEW DELHI

(iii) print(myAddress.count(‘New’))
Ans:
2

(iv) print(myAddress.find(‘New’))
Ans: 
5

(v) print(myAddress.rfind(‘New’))
Ans:
21

(vi) print(myAddress.split(‘,’))
Ans:
[‘WZ-1’, ‘New Ganga Nagar’, ‘New Delhi’]

(vii) print(myAddress.split(‘ ‘))
Ans:
[‘WZ-1,New’, ‘Ganga’, ‘Nagar,New’, ‘Delhi’]

(viii) print(myAddress.replace(‘New’,’Old’))
Ans: 
WZ-1,Old Ganga Nagar,Old Delhi

(ix) print(myAddress.partition(‘,’))
Ans:
(‘WZ-1’ , ‘ , ‘ , ‘New Ganga Nagar,New Delhi’)

(x) print(myAddress.index(‘Agra’))
Ans:
ValueError : Substring not found

Programming Problems

Q1: 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). 
Ans: 

str = input("Enter any String")

 al=d=sp=w=s=0

 for i in str:

   if i.isalpha():

     al = al +1

   elif i.isdigit():

     d = d + 1

   elif i.isspace():

     sp = sp + 1

   else:

     s=s+1

 w = len(str.split())

 print("Total number of Characters are              : " , len(str))

 print("Total number of Alphabets are               : " , al)

 print("Total number of Digits are                      : " , d)

 print("Total number of Spaces are                    : " , sp)

 print("Total number of Special Characters are  : " , s)

 print("Total number of Words are                     : " , w)

Q2: 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) 
Ans: 

def Tcase(str):

   if len(str.split())>1:

     print(str.title())

   else:

     print("String has one word only")

 str = input("Enter any String")

 Tcase(str)

Q3: 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.
Ans:

def deleteChar(str,ch):

    nstr=str.replace(ch,"")

    print("New String after deleting all characters is  :",nstr)

str = input("Enter any String")

ch = input("Enter the character to be deleted")

deleteChar(str,ch)

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

def sumdigit(str):

     s=0

     for i in str:

         if i.isdigit():

             s = s + int(i)

     print("Sum of digit is  : ",s)

str = input("Enter any String")

sumdigit(str)

Q5: 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.
Ans:

def repspace(str1):

     nst=str1.replace(" ",'-')

     return nst

str = input("Enter any String")

nstr = repspace(str)

print("New String after replacing all spaces is  :" , nstr)

The document Strings NCERT Solutions | Computer Science for Class 11 - Humanities/Arts is a part of the Humanities/Arts Course Computer Science for Class 11.
All you need of Humanities/Arts at this link: Humanities/Arts
33 docs|11 tests
Related Searches

Exam

,

pdf

,

shortcuts and tricks

,

Strings NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

,

past year papers

,

Extra Questions

,

Viva Questions

,

practice quizzes

,

Summary

,

Sample Paper

,

ppt

,

Important questions

,

Objective type Questions

,

Strings NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

,

MCQs

,

study material

,

Strings NCERT Solutions | Computer Science for Class 11 - Humanities/Arts

,

video lectures

,

Free

,

Previous Year Questions with Solutions

,

Semester Notes

,

mock tests for examination

;