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!