Python 程式,使用給定字串中的集合統計母音數量


我們使用給定字串中的集合來統計母音數量。假設我們擁有以下輸入 -

jackofalltrades

輸出應如下所示,統計母音數量 -

65

使用給定字串中的集合統計母音數量

我們將使用給定的字串中的集合統計母音數量 -

示例

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

更新於:11-8-2022

1 千次瀏覽

職業起步

完成課程即可獲得認證

開始
廣告
© . All rights reserved.