Чтобы подсчитать количество гласных в строке с помощью Python, вы можете использовать несколько методов. Вот несколько подходов:
Метод 1: перебор строки
def count_vowels(string):
vowels = "aeiouAEIOU"
count = 0
for char in string:
if char in vowels:
count += 1
return count
string = "count how many vowels in a string python"
vowel_count = count_vowels(string)
print("Number of vowels:", vowel_count)
Метод 2: использование понимания списка
def count_vowels(string):
vowels = "aeiouAEIOU"
vowel_count = len([char for char in string if char in vowels])
return vowel_count
string = "count how many vowels in a string python"
vowel_count = count_vowels(string)
print("Number of vowels:", vowel_count)
Метод 3: регулярные выражения
import re
def count_vowels(string):
vowel_count = len(re.findall(r'[aeiouAEIOU]', string))
return vowel_count
string = "count how many vowels in a string python"
vowel_count = count_vowels(string)
print("Number of vowels:", vowel_count)