50 Python String Programs with Solutions (WB
Class XII)
1. Print a string
s='Hello World'
print(s)
2. Input and display string
s=input('Enter string: ')
print(s)
3. Length of string
s=input()
print(len(s))
4. Uppercase
s=input()
print([Link]())
5. Lowercase
s=input()
print([Link]())
6. Capitalize words
s=input()
print([Link]())
7. Count characters
s=input()
print(len(s))
8. Reverse string
s=input()
print(s[::-1])
9. Palindrome
s=input()
print('Palindrome' if s==s[::-1] else 'Not Palindrome')
10. Count vowels
s=input().lower()
print(sum(1 for c in s if c in 'aeiou'))
11. Count consonants
s=input().lower()
print(sum(1 for c in s if [Link]() and c not in 'aeiou'))
12. Count digits
s=input()
print(sum(1 for c in s if [Link]()))
13. Count special chars
s=input()
print(sum(1 for c in s if not [Link]() and c!=' '))
14. Count uppercase
s=input()
print(sum(1 for c in s if [Link]()))
15. Count lowercase
s=input()
print(sum(1 for c in s if [Link]()))
16. Count spaces
s=input()
print([Link](' '))
17. Frequency of character
s=input(); ch=input()
print([Link](ch))
18. Display vowels
s=input()
for c in s:
if [Link]() in 'aeiou': print(c,end=' ')
19. Count each vowel
s=input().lower()
for v in 'aeiou': print(v,[Link](v))
20. Count words
s=input()
print(len([Link]()))
21. Search character
s=input(); ch=input()
print(ch in s)
22. Search word
s=input(); w=input()
print(w in s)
23. First occurrence
s=input(); ch=input()
print([Link](ch))
24. Last occurrence
s=input(); ch=input()
print([Link](ch))
25. Substring exists
s=input(); sub=input()
print(sub in s)
26. Count word occurrence
s=input(); w=input()
print([Link]().count(w))
27. Position of word
s=input(); w=input()
print([Link](w))
28. All positions of char
s=input(); ch=input()
for i,c in enumerate(s):
if c==ch: print(i,end=' ')
29. Starts with word
s=input(); w=input()
print([Link](w))
30. Ends with word
s=input(); w=input()
print([Link](w))
31. Replace word
s=input(); a=input(); b=input()
print([Link](a,b))
32. Remove spaces
s=input()
print([Link](' ',''))
33. Remove extra spaces
s=input()
print(' '.join([Link]()))
34. Insert word
s=input(); w=input()
print(w+' '+s)
35. Delete character
s=input(); p=int(input())
print(s[:p]+s[p+1:])
36. Delete word
s=input(); w=input()
print([Link](w,''))
37. First word
s=input()
print([Link]()[0])
38. Last word
s=input()
print([Link]()[-1])
39. Swap first and last chars
s=input()
print(s[-1]+s[1:-1]+s[0])
40. Separate chars
s=input()
print('-'.join(s))
41. Each char separate line
s=input()
for c in s: print(c)
42. Words beginning with vowel
s=input(); c=0
for w in [Link]():
if w[0].lower() in 'aeiou': c+=1
print(c)
43. Words ending with consonant
s=input(); c=0
for w in [Link]():
if w[-1].lower() not in 'aeiou': c+=1
print(c)
44. Palindrome words
s=input()
for w in [Link]():
if w==w[::-1]: print(w)
45. Longest word
s=input(); w=[Link]()
print(max(w,key=len))
46. Shortest word
s=input(); w=[Link]()
print(min(w,key=len))
47. Alphabetical order
s=input(); w=[Link](); [Link](); print(w)
48. Word frequency
s=input().split()
for w in set(s): print(w,[Link](w))
49. Title case without title()
s=input(); print(' '.join([Link]() for w in [Link]()))
50. Simple cipher +1
s=input(); print(''.join(chr(ord(c)+1) for c in s))