Pythonで文字列の母音を数えて表示する
文字列が与えられたら、母音である文字の数を分析してみましょう。
セット付き
まず、すべての個別の一意の文字を見つけてから、それらが母音を表す文字列に存在するかどうかをテストします。
例
stringA = "Tutorialspoint is best"
print("Given String: \n",stringA)
vowels = "AaEeIiOoUu"
# Get vowels
res = set([each for each in stringA if each in vowels])
print("The vlowels present in the string:\n ",res) 出力
上記のコードを実行すると、次の結果が得られます-
Given String:
Tutorialspoint is best
The vlowels present in the string:
{'e', 'i', 'a', 'o', 'u'} fromkeysを使用
この関数を使用すると、文字列を辞書として扱うことにより、文字列から母音を抽出できます。
例
stringA = "Tutorialspoint is best"
#ignore cases
stringA = stringA.casefold()
vowels = "aeiou"
def vowel_count(string, vowels):
# Take dictionary key as a vowel
count = {}.fromkeys(vowels, 0)
# To count the vowels
for v in string:
if v in count:
# Increasing count for each occurence
count[v] += 1
return count
print("Given String: \n", stringA)
print ("The count of vlowels in the string:\n ",vowel_count(stringA, vowels)) 出力
上記のコードを実行すると、次の結果が得られます-
Given String:
tutorialspoint is best
The count of vlowels in the string:
{'a': 1, 'e': 1, 'i': 3, 'o': 2, 'u': 1}> -
指定された文字列のセットを使用して母音の数をカウントするPythonプログラム
この記事では、以下に示す問題ステートメントの解決策について学習します。 問題の説明 −文字列が与えられたので、与えられた文字列のセットを使用して母音の数を数える必要があります。 ここでは、文字列全体をトラバースして、各文字が母音であるかどうかを確認し、カウントをインクリメントします。 次に、以下の実装の概念を観察しましょう- 例 def vowel_count(str): count = 0 #string of vowels vowel = "aeiouAEIOU" &nbs
-
Pythonを使用して文字列内の母音の数を数える方法は?
すべての母音を含む文字列オブジェクトを宣言します。 >>> vowels='aeiou' カウント変数を0に初期化するように設定します >>> count=0 入力文字列の各文字が母音文字列に属しているかどうかを確認します。はいの場合、カウントをインクリメントします >>> string='Hello How are you?' >>> for s in string: if s in vowels: c