2024-06-28 11:22:01 发布
网友
我想数一数一个字符串中的元音字母,然后这样做
count =0 t='aeiou' s='azcbobobegghaklhbjkhiohvghfaaaa' for i in s: if i in t: count =count +1 print count
在采取这种方法之前,我试着
但在这里我面临的问题是,一旦char被当作'a'并且整个循环被执行,现在char的值变为'e',所以如果字符串中还有另一个a,它就不被计算在内了,我该如何解决这个问题呢
您可以使用count()方法
count()
比如:
string = "aaabbaacdeef" string.count('a') # returns the count of 'a' in string >>> 5
您可以使用
count()
方法比如:
相关问题 更多 >
编程相关推荐