Advertisement
brandblox

python lab(19/10/2024)

Mar 19th, 2024
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. #Count vowels, consonants and blanks
  2. def count_vowels_consonants_blanks(string):
  3.     vowels = 'aeiouAEIOU'
  4.     consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
  5.     blanks = ' '
  6.  
  7.     vowel_count = 0
  8.     consonant_count = 0
  9.     blank_count = 0
  10.  
  11.     for char in string:
  12.         if char in vowels:
  13.             vowel_count += 1
  14.         elif char not in consonants:
  15.             consonant_count += 1
  16.         elif char in blanks:
  17.             blank_count += 1
  18.  
  19.     return vowel_count, consonant_count, blank_count
  20.  
  21. input_string = input("Enter a string: ")
  22. vowels, consonants, blanks = count_vowels_consonants_blanks(input_string)
  23. print("Vowels:", vowels)
  24. print("Consonants:", consonants)
  25. print("Blanks:", blanks)
  26.  
  27. #Output:
  28. Enter a string: Arijit is my name
  29. Vowels: 6
  30. Consonants: 8
  31. Blanks: 3
  32.  
  33. #find substring in a string
  34. def find_substring(string, substring):    
  35.     index = string.find(substring)
  36.     if index != -1:
  37.         print(f"'{substring}' found at index {index} using find() method.")
  38.     else:
  39.         print(f"'{substring}' not found using find() method.")
  40.  
  41.  
  42. input_string = input("Enter the main string: ")
  43. input_substring = input("Enter the substring to find: ")
  44.  
  45. find_substring(input_string, input_substring)
  46.  
  47.  
  48. #Output:
  49. Enter the main string: i live in India
  50. Enter the substring to find: India
  51. 'India' found at index 10 using find() method.
  52.  
  53.  
  54. #print reverse and check palindrome
  55. def is_palindrome(string):
  56.     reversed_string = string[::-1]
  57.     if string == reversed_string:
  58.         return True, reversed_string
  59.     else:
  60.         return False, reversed_string
  61.  
  62. input_string = input("Enter a string: ")
  63. check, reversed_string = is_palindrome(input_string)
  64.  
  65. print("Original string:", input_string)
  66. print("Reversed string:", reversed_string)
  67.  
  68. if check:
  69.     print("The string is a palindrome.")
  70. else:
  71.     print("The string is not a palindrome.")
  72.  
  73.  
  74. #Output:
  75. Enter a string: malayalam
  76. Original string: malayalam
  77. Reversed string: malayalam
  78. The string is a palindrome.
  79.  
  80. #Print pattern
  81. word = "INDIA"
  82. for i in range(len(word), 0, -1):
  83.     print(word[:i])
  84.  
  85. #Ouput:
  86. INDIA
  87. INDI
  88. IND
  89. IN
  90. I
  91.  
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement