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

Contents

## 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)])

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

iii. print(mySubject[::2])

iv. print(mySubject[len(mySubject)-1])

v. print(2*mySubject)

vi. print(mySubject[::-2])

vii. print(mySubject[:3] + mySubject[3:])

viii. print(mySubject.swapcase())

ix. print(mySubject.startswith(‘Comp’))

x. print(mySubject.isalpha())

2. Consider the following string myAddress:
myAddress = “WZ-1,New Ganga Nagar,New Delhi”

What will be the output of following string operations :

Answer – wz-1,new ganga nagar,new delhi

Answer – WZ-1,NEW GANGA NAGAR,NEW DELHI

Answer – [‘WZ-1’, ‘New Ganga Nagar’, ‘New Delhi’]

Answer – [‘WZ-1,New’, ‘Ganga’, ‘Nagar,New’, ‘Delhi’]

Answer – WZ-1,Old Ganga Nagar,Old Delhi

Answer – (‘WZ-1’, ‘,’, ‘New Ganga Nagar,New Delhi’)

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

##### Computer Science Class 11 NCERT Solutions

Computer Science Class 11 Practical Questions and Answers

error: Content is protected !!