Chapter – 9
String Manipulation Programs
Using String Built in Function
1. Write a program to count the length of string using in-built function.
string = input("Enter the string")
length = len(string)
print("Length of string is ",length)
2. Write a program to accept string and display total number of alphabets using in-
built function.
string = input("Enter the string")
length = len(string)
count=0
for i in string:
if i.isalpha():
count=count+1
print("Total no. of alphabet present in the string is ",count)
3. Write a program to accept a string and display the sum of the digits, if any
present in string using built-in function
for example: input string : My position is 1st and my friend come on 4th
string = input("Enter the string")
length = len(string)
sum=0
for i in string:
if i.isdigit():
sum=sum+int(i)
print("Sum of digits present in the string is ",sum)
4. Write a program to accept a string and convert it into lowercase.
Ans. str1 = input("Enter any string")
print(str1.lower())
or
string = input("enter the string")
string2=""
for i in string:
if i.isupper():
string2+=i.lower()
else:
string2+=i
print("Lower String is", string2)
5. Write a program to count the number of lowercase and uppercase character in a
string accepted from the user.
string = input("enter the string")
Lcount=0
Ucount=0
for i in string:
if i.islower():
Lcount=Lcount+1
if i.isupper():
Ucount=Ucount+1
print("Total uppercase letter present in string is ",Ucount)
print("Total lowercase letter present in string is ",Lcount)
6. Write a program to accept a string and display each word and it's length.
string = input("enter the string")
lst = string.split()
print(lst)
for word in lst:
x=len(word)
print(word,"length is", x)
7. Write a program to accept a string and display string with capital letter of each
word. for example, if input string is : welcome to my blog
output string : Welcome To My Blog
string = input("enter the string")
lst = string.split()
string2=""
for word in lst:
string2=string2 +" "+ word.capitalize()
print("Original String is", string)
print("Capitalized String is", string2)
8. Write a program to replace all the word 'do' with 'done' in the following string.
str1 = "I do you do and we all will do"
string = input("enter the string")
string2 = string.replace("do","done")
print("Original String is", string)
print("Replaced String is", string2)
9. Write a program to accept a string and display it in upper case.
str=input("Enter any String :")
print(str.upper( ))
10. Write a program to accept a string and display the entire string with first and
last character in upper case.
Ans. str = input("Enter any String")
print(str[0].upper() + str[1:-1] + str[-1].upper())
11. Write a function in python which accept a string as argument and display total
number of digits.
string = input("enter the string")
count=0
for i in string:
if i.isdigit():
count=count+1
print("Total digits present in the string is", count)
12. Write a program to check whether an input string is palindrome or not.
str = input("Enter any String")
strrev =""
for i in range(len(str),0,-1):
strrev=strrev + str[i-1]
if str.lower() == strrev.lower():
print("Palindrome")
else:
print("Not a palindrome")
13. Write a program to count the number of words in the input string.
string = input("Enter any String")
lst=string.split()
count=len(lst)
print("Total number of words are ", count)
14. Write a program to display the last word of the string accepted from user.
string = input("Enter any String")
lst=str.split()
print(lst[-1])
15. Write a program to accept a string and display the ASCII value of each
character.
str=input("Enter any String :")
for i in str:
print("ASCII value of ",i,"is",ord(i))
16. Write a program to accept a string and replace all spaces by ‘#’ symbol.
str=input("Enter any String :")
str1=""
for i in str:
if i.isspace():
str1=str1+'#'
else:
str1=str1+i
print("Output String is :",str1)
17. Write a program to accept a string and count the frequency of each vowel.
str1=input("Enter String :")
print("frequency of vowel 'a' is :",str1.count('a'))
print("frequency of vowel 'e' is :",str1.count('e'))
print("frequency of vowel 'i' is :",str1.count('i'))
print("frequency of vowel 'o' is :",str1.count('o'))
print("frequency of vowel 'u' is :",str1.count('u'))
18. Write a program to accept a string and display the string in Title case. (first
alphabet of each word in upper case)
str=input("Enter any String :")
print(str.title())
19. Write a program to accept a string and display the string with changed
case.(Change upper case alphabet to lower case and vice versa)
str=input("Enter any String :")
print(str.swapcase())
20. Write a program to accept a string and display the word which has vowel ‘a’ in
it.
Ans.
str = input("Enter any String")
L=str.split()
for i in L:
if 'a' in i:
print(i)
21. Write a program to accept the first name from the user and display it as much
times as its length.
Ans.
str = input("Enter first name")
for i in range(len(str)):
print(str)
22. Write a program to accept a string from the user and display the string without space.
Ans.
str = input("Enter any string")
str1=""
for i in str:
if i.isspace()==False:
str1=str1+i
print(str1)
23. Write a program to accept a string and substring from the user and display the
frequency of substring in string.
str = input("Enter any string")
substr=input("Enter any substring")
print(str.count(substr))
24. Write a program to accept a string and substring from the user and check whether the
substring is existing/present in string or not.
.
str = input("Enter any string")
substr=input("Enter any substring")
if (str.find(substr)!=-1):
print("Substring is present")
else:
print("Substring is not present")
25. Write a program that takes a string with multiple words and then capitalizes the first
letter of each word and forms a new string out of it.
Ans : # Simple method using function split() and capitalize()
string = input("Enter a string :- ")
lst = string.split()
for i in lst:
print (i.capitalize(), end = " ")
26. Python Program to calculate the number of digits and letters in a string
string=input("Enter string:")
count1=0
count2=0
for i in string:
if(i.isdigit()):
count1=count1+1
if(i.isalpha()):
count2=count2+1
print("The number of digits is:")
print(count1)
print("The number of characters is:")
print(count2)