Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Count vowels, consonants and blanks
- def count_vowels_consonants_blanks(string):
- vowels = 'aeiouAEIOU'
- consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
- blanks = ' '
- vowel_count = 0
- consonant_count = 0
- blank_count = 0
- for char in string:
- if char in vowels:
- vowel_count += 1
- elif char not in consonants:
- consonant_count += 1
- elif char in blanks:
- blank_count += 1
- return vowel_count, consonant_count, blank_count
- input_string = input("Enter a string: ")
- vowels, consonants, blanks = count_vowels_consonants_blanks(input_string)
- print("Vowels:", vowels)
- print("Consonants:", consonants)
- print("Blanks:", blanks)
- #Output:
- Enter a string: Arijit is my name
- Vowels: 6
- Consonants: 8
- Blanks: 3
- #find substring in a string
- def find_substring(string, substring):
- index = string.find(substring)
- if index != -1:
- print(f"'{substring}' found at index {index} using find() method.")
- else:
- print(f"'{substring}' not found using find() method.")
- input_string = input("Enter the main string: ")
- input_substring = input("Enter the substring to find: ")
- find_substring(input_string, input_substring)
- #Output:
- Enter the main string: i live in India
- Enter the substring to find: India
- 'India' found at index 10 using find() method.
- #print reverse and check palindrome
- def is_palindrome(string):
- reversed_string = string[::-1]
- if string == reversed_string:
- return True, reversed_string
- else:
- return False, reversed_string
- input_string = input("Enter a string: ")
- check, reversed_string = is_palindrome(input_string)
- print("Original string:", input_string)
- print("Reversed string:", reversed_string)
- if check:
- print("The string is a palindrome.")
- else:
- print("The string is not a palindrome.")
- #Output:
- Enter a string: malayalam
- Original string: malayalam
- Reversed string: malayalam
- The string is a palindrome.
- #Print pattern
- word = "INDIA"
- for i in range(len(word), 0, -1):
- print(word[:i])
- #Ouput:
- INDIA
- INDI
- IND
- IN
- I
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement