String Manipulation in Python Class 11 Solutions

Teachers and Examiners (CBSESkillEduction) collaborated to create the String Manipulation in Python Class 11 Solutions. All the important Information are taken from the NCERT Textbook Computer Science (083) class 11.

String Manipulation in Python Class 11 Questions and Answers

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)])
Answer – Computer Science

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

iii. print(mySubject[::2])
Answer – Cmue cec

iv. print(mySubject[len(mySubject)-1])
Answer – e

v. print(2*mySubject)
Answer – Computer ScienceComputer Science

vi. print(mySubject[::-2])
Answer – eniSrtpo

vii. print(mySubject[:3] + mySubject[3:])
Answer – Computer Science

viii. print(mySubject.swapcase())
Answer – cOMPUTER sCIENCE

ix. print(mySubject.startswith(‘Comp’))
Answer – True

x. print(mySubject.isalpha())
Answer – 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())
Answer – wz-1,new ganga nagar,new delhi

ii. print(myAddress.upper())
Answer – WZ-1,NEW GANGA NAGAR,NEW DELHI

iii. print(myAddress.count(‘New’))
Answer – 2

iv. print(myAddress.find(‘New’))
Answer – 5

v. print(myAddress.rfind(‘New’))
Answer – 21

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

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

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

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

x. print(myAddress.index(‘Agra’))
Answer – ValueError: substring not found

String Manipulation in Python Class 11 Solutions

3. 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 –
str = input(“Enter your text : “)
Alpha = 0
Digit = 0
Special = 0
words = 0
for ch in str:
if ch.isalpha():
Alpha += 1
elif ch.isdigit():
Digit += 1
else:
Special += 1

for ch1 in str:
if ch1.isspace():
words += 1
print(“Alphabets: “,Alpha)
print(“Digits: “,Digit)
print(“Special Characters: “,Special)
print(“Words in the Input :”,(words + 1))

Output
Enter your text : Hello how are you
Alphabets: 14
Digits: 0
Special Characters: 3
Words in the Input : 4

4. 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 –
def convertToTitle(string):
titleString = string.title();
print(titleString)

str = input(“Type your Text : “)
Space = 0
for b in str:
if b.isspace():
Space += 1

if(str.istitle()):
print(“The String is already in title case”)
elif(Space > 0):
convertToTitle(str)
else:
print(“One word String”)

Output
Type your Text : Welcome to my school
Welcome To My School

5. 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 –
def deleteChar(string,char):
str = “”
for ch in string:
if ch != char:
str+=ch
return str

string = input(“Enter a string: “)
char = (input(“Enter a character: “))[0]
print(deleteChar(string,char))

6. Input a string having some digits. Write a function to return the sum of digits present in this string.
Answer –
def myfunction(string):
sum = 0
for a in string:
if(a.isdigit()):
sum += int(a)
return sum

str = input(“Enter any string with digits : “)
result = myfunction(str)
print(“The sum of digits in the string : “,result)

Output
Enter any string with digits : Welcome 225
The sum of digits in the string : 9

7. 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 –
def myfunction():
string = input(“Enter a sentence: “)
str = string.replace(” “,”-“)
print(str)

myfunction()

Output
Enter a sentence: Welcome to my school
Welcome-to-my-school

omputer Science Class 11 Notes
Computer Science Class 11 MCQ
Computer Science Class 11 NCERT Solutions

Computer Science Class 11 Practical Questions and Answers

error: Content is protected !!