我想计算一个字符串中元音的数量并按照这样做
count =0
t='aeiou'
s='azcbobobegghaklhbjkhiohvghfaaaa'
for i in s:
if i in t:
count =count +1
print count
在采用这种方法之前,我试图像
那样做def isVowel(s):
count=0
for char in 'aeiou':
t=s.find(char)
if t>=0:
count=count+1
s=s[t:]
return count
print isVowel('azcbobobegghakl')
但在这里,我面临的问题是,一旦将char作为' a'然后执行整个循环,现在char的值变为' e'所以,如果字符串中有另一个a,则不计算。如何在这种方法中解决这个问题
答案 0 :(得分:1)
您可以使用count()
方法
像:
string = "aaabbaacdeef"
string.count('a') # returns the count of 'a' in string
>>> 5