Chapter 8 - String Programs (NCERT)
Chapter 8 - String Programs (NCERT)
NCERT Programs
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).
Program
userInput = input("Write a sentence: ")
totalChar = len(userInput)
print("Total Characters: ",totalChar)
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)
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1
print("Total Words in the Input :",(totalSpace + 1))
Output
Write a sentence: welcome to computer 2024****
Total Characters: 28
Total Alphabets: 17
Total Digits: 4
Total Special Characters: 7
Total Words in the Input : 4
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)
def convertToTitle(string):
titleString = string.title();
print("The input string in title case is:",titleString)
userInput = input("Write a sentence: ")
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1
if(userInput.istitle()):
print("The String is already in title case")
elif(totalSpace > 0):
convertToTitle(userInput)
else:
print("The String is of one word only")
output
Write a sentence: good evening all
The input string in title case is: Good Evening All
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.
Program
def deleteChar(string,char):
newString = ' '
for a in string:
if a == char:
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 Morning
Input the character to delete from the string: o
The new string after deleting all occurrences of o is: G d M rning
4. Input a string having some digits. Write a function to return the sum of digits present in this string
Program
def sumDigit(string):
sum = 0
for a in string:
if(a.isdigit()):
sum += int(a)
return sum
userInput = input("Enter any string with digits: ")
result = sumDigit(userInput)
print("The sum of all digits in the string '",userInput,"' is:",result)
Output
Enter any string with digits: cost is 4320
The sum of all digits in the string ' cost is 4320 ' is: 9
5. 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
Program
def replaceChar(string):
return string.replace(' ','-')
userInput = input("Enter a sentence: ")
result = replaceChar(userInput)
print("The new sentence is:",result)
Output
Enter a sentence: Welcome to python
The new sentence is: Welcome-to-python