Python 程序使用 set 计算给定字符串中元音的数量
programmingpythonserver side programming更新于 2025/3/14 13:00:17
我们将使用 set 计算给定字符串中元音的数量。假设我们有以下输入 -
jackofalltrades
输出应为以下内容,计算元音的数量 -
65
使用 set 计算给定字符串中元音的数量
我们将使用 set 计算给定字符串中元音的数量 -
示例
def vowelFunc(str): c = 0 # Create a set of vowels s="aeiouAEIOU" v = set(s) # Loop to traverse the alphabet in the given string for alpha in str: # If alphabet is present # in set vowel if alpha in v: c = c + 1 print("Count of Vowels = ", c) # Driver code str = input("Enter the string = ") vowelFunc(str)
输出
Enter the string = howareyou Count of Vowels = 5
使用集合计算给定字符串中的元音数量,而不使用函数
我们将使用集合计算元音数量,而不使用函数 −
示例
# string to be checked myStr = "masterofnone" count = 0 print("Our String = ",myStr) # Vowel Set vowels = set("aeiouAEIOU") # Loop through, check and count the vowels for alpha in myStr: if alpha in vowels: count += 1 print("Count of Vowels = ",count)
输出
Our String = masterofnone Count of Vowels = 5
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

