
Important String Programs in Python for exams are given below:
Q11. Write a program to display the largest word from the string.
Ans. str=input("Enter any String :") L = str.split() max=0 b="" for i in L: if len(i) > max: b=i max=len(i) print("Longest word is ",b)
Q12. Write a program to accept a string and display it in upper case.
Ans. str=input("Enter any String :") print(str.upper( )) #Execution of program is : Enter any String :String Programs STRING PROGRAMS
Q13. Write a program to display the unique words from the string.
Ans. str=input("Enter any String :") L=str.split( ) L1=[ ] for i in L: if i not in L1: L1.append(i) str="" for i in L1: str=str+" "+i print("String with unique words are :",str) Execution of Program Enter any String : string program practice string program String with unique words are : string program practice
Q14. Write a program to accept a string and display the ascii value of each character.
Ans. str=input("Enter any String :") for i in str: print("ASCII value of ",i,"is",ord(i)) #Execution of the program Enter any String : String Programs ASCII value of S is 83 ASCII value of t is 116 ASCII value of r is 114 ASCII value of i is 105 ASCII value of n is 110 ASCII value of g is 103 ASCII value of is 32 ASCII value of P is 80 ASCII value of r is 114 ASCII value of o is 111 ASCII value of g is 103 ASCII value of r is 114 ASCII value of a is 97 ASCII value of m is 109 ASCII value of s is 115
Q15. Write a program to accept a string and replace all spaces by ‘#’ symbol.
Ans. str=input("Enter any String :") str1="" for i in str: if i.isspace(): str1=str1+'#' else: str1=str1+i print("Output String is :",str1) #Execution of program Enter any String :practice string programs Output String is : practice#string#programs
Q16. Write a program to accept two strings from the user and display the common words.(Ignore case)
Ans. str1=input("Enter first String :") str2=input("Enter second String :") L1=str1.split() L2=str2.split() for i in L1: if i in L2: print(i) #Execution of program is : Enter first String : program string programs Enter second String : program program
Q17. Write a program to accept a string and count the frequency of each vowel.
Ans 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')) #Execution of program Enter String :Important String Programs frequency of vowel 'a' is : 2 frequency of vowel 'e' is : 0 frequency of vowel 'i' is : 1 frequency of vowel 'o' is : 2 frequency of vowel 'u' is : 0
Q18. Write a program to display the smallest word from the string.
Ans. str=input("Enter any String :") L = str.split() min=50 #we think that length of any word in the string is not larger than 50 (can give any other number) b="" for i in L: if len(i) < min: b=i min=len(i) print("Smallest word is : ",b) #Execution of program: Enter any String :important string programs Smallest word is : string
Q19. Write a program to accept a string and display the string in Title case. (first alphabet of each word in upper case)
Ans. str=input("Enter any String :") print(str.title()) #Execution of program is : Enter any String :my name is amit My Name Is Amit
Q20. Write a program to accept a string and display the string with changed case.(Change upper case alphabet to lower case and vice versa)
Ans. str=input("Enter any String :") print(str.swapcase()) #Execution of program is : Enter any String :My name is Amit mY NAME IS aMIT
Disclaimer : I tried to give you the correct code of all the string programs, but if you feel that there is/are mistakes in the string programs given above, you can directly contact me at csiplearninghub@gmail.com. Also Share your feedback so that I can give better content to you.
Important Links :
Class 12 Computer Science Sample Paper 2020-2021.
Class 12 Computer Science Sample Paper Marking Scheme
Class 12 Computer Science Test Series







