Python 使用集合在给定字符串中计算元音字母

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

使用set在给定的字符串中计算元音字母的数量,不使用函数

我们将使用set来计算元音字母的数量,而不使用函数 –

示例

# 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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程