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 :
mySubject = "Computer Science"
print(mySubject[0:len(mySubject)])
print(mySubject[-7:-1])
print(mySubject[::2])
print(mySubject[len(mySubject)-1])
print(2 * mySubject)
print(mySubject[::-2])
print(mySubject[:3] + mySubject[3:])
print(mySubject.swapcase())
print(mySubject.startswith('Comp'))
print(mySubject.isalpha())
Output:
Computer Science
Scienc
Cmue cec
e
Computer ScienceComputer Science
eniSrtpo
Computer Science
cOMPUTER sCIENCE
True
False
2. Consider the following string myAddress:
myAddress = “WZ-1,New Ganga Nagar,New Delhi”
What will be the output of following string operations :
myAddress = "WZ-1,New Ganga Nagar,New Delhi"
print(myAddress.lower())
print(myAddress.upper())
print(myAddress.count('New'))
print(myAddress.find('New'))
print(myAddress.rfind('New'))
print(myAddress.split(','))
print(myAddress.split(' '))
print(myAddress.replace('New', 'Old'))
print(myAddress.partition(','))
Output:
wz-1,new ganga nagar,new delhi
WZ-1,NEW GANGA NAGAR,NEW DELHI
2
5
21
['WZ-1', 'New Ganga Nagar', 'New Delhi']
['WZ-1,New', 'Ganga', 'Nagar,New', 'Delhi']
WZ-1,Old Ganga Nagar,Old Delhi
('WZ-1', ',', 'New Ganga Nagar,New Delhi')
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 –
text = input("Enter your text: ")
Alpha = Digit = Special = 0
for ch in text:
if ch.isalpha():
Alpha += 1
elif ch.isdigit():
Digit += 1
elif not ch.isspace():
Special += 1
words = text.count(' ') + 1
print("Total Characters:", len(text))
print("Alphabets:", Alpha)
print("Digits:", Digit)
print("Special Characters:", Special)
print("Words in the Input:", words)
Output:
Enter your text: Hello how are you?
Total Characters: 18
Alphabets: 14
Digits: 0
Special Characters: 4
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 convert_to_title(string):
title_string = string.title() # Converts to title case
print(title_string)
text = input("Type your Text: ")
if text.istitle():
print("The String is already in title case")
elif " " in text:
convert_to_title(text)
else:
print("One word String")
Output:
Type your Text: welcome to my school
Welcome To My School
Type your Text: Welcome To My School
The String is already in title case
Type your Text: Python
One word String
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 delete_char(string, char):
return string.replace(char, '')
string = input("Enter a string: ")
char = input("Enter a character to delete: ")[0]
print("Modified String:", delete_char(string, char))
Output:
Enter a string: Hello, how are you?
Enter a character to delete: o
Modified String: Hell, hw are yu?
6. Input a string having some digits. Write a function to return the sum of digits present in this string.
Answer –
def sum_of_digits(string):
return sum(int(ch) for ch in string if ch.isdigit())
text = input("Enter any string with digits: ")
result = sum_of_digits(text)
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 replace_spaces(sentence):
return sentence.replace(" ", "-")
text = input("Enter a sentence: ")
modified_text = replace_spaces(text)
print("Modified Sentence:", modified_text)
Output:
Enter a sentence: Welcome to my school
Modified Sentence: Welcome-to-my-school
Computer Science Class 11 Questions and Answers
- Computer Systems and Organisation
- Introduction to problem solving
- Getting Started with Python
- Flow of Control statements in Python
- String in Python
- Lists in Python
- Tuples and Dictionary in Python
- Society, Law and Ethics
Disclaimer: We have taken an effort to provide you with the accurate handout of “String Manipulation in Python Class 11 Solutions“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Computer Science Class 11 NCERT Textbook and CBSE Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination.
Images and content shown above are the property of individual organizations and are used here for reference purposes only.
For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in