Python 编写一个计算字符和元音的函数

Python 编写一个计算字符和元音的函数,python,string,function,input,call,Python,String,Function,Input,Call,我需要编写一个函数,对用户输入的字符串中的字符和元音进行计数,并编写一个例程来调用该函数并显示以下内容: $ ./count_all.py Enter some words: The sun rises in the East and sets in the West 13 letters in 47 are vowels. 执行此操作的最佳方法是什么?不可读1行: import re stringToTest = "a9821e89asdi89123o9812378u" print(str(

我需要编写一个函数,对用户输入的字符串中的字符和元音进行计数,并编写一个例程来调用该函数并显示以下内容:

$ ./count_all.py
Enter some words: The sun rises in the East and sets in the West
13 letters in 47 are vowels.
执行此操作的最佳方法是什么?

不可读1行:

import re
stringToTest = "a9821e89asdi89123o9812378u"
print(str(len(re.findall(r"a|e|i|o|u", stringToTest, re.IGNORECASE))) + " letters in " + str(len(stringToTest)) + " are vowels")
#6 letters in 26 are vowels
可读形式

import re
stringToTest = "a9821e89asdi89123o9812378u"

stringLength = len(stringToTest) #length of stirng, this is how many characters we have
regexResult = re.findall(r"a|e|i|o|u", stringToTest, re.IGNORECASE) #match for a or e or i or o or u

numberVowels = len(regexResult) #our number of vowels is how many regex matches we got


print(str(numberVowels) + " vowels in " + str(stringLength) + " characters")

#6 vowels in 26 characters
可能重复的