NCERT Chapter 8 Python String Questions with Answers
Summary: NCERT Chapter 8 String in Python
Summary: NCERT Chapter 8
A string is a sequence of characters enclosed in single, double or triple quotes.
Indexing is used for accessing individual characters within a string.
The first character has the index 0 and the last character has the index n-1 where n is the length of the string.
The negative indexing ranges from -n to -1.
Strings in Python are immutable, i.e., a string cannot be changed after it is created.
Membership operator in takes two strings and returns True if the first string appears as a substring in the second else returns False. Membership operator ‘not in’ does the reverse.
Retrieving a portion of a string is called slicing. This can be done by specifying an index range. The slice operation str1[n : m] returns the part of the string str1 starting from index n (inclusive) and ending at m (exclusive).
Each character of a string can be accessed either using a for loop or while loop.
There are many built-in functions for working with strings in Python.
EXERCISE : NCERT Chapter 8
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
iii. print(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
EXERCISE : NCERT Chapter 8
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
EXERCISE : NCERT Chapter 8
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)
EXERCISE : NCERT Chapter 8
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)
EXERCISE : NCERT Chapter 8
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)
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series