使用python计算字符串中字母的出现次数

时间:2012-03-23 03:14:00

标签: python string

  

可能重复:
  Count occurrence of a character in a Python string

我对此毫无希望地

我需要编写一个函数,它将一串Ms和Fs作为输入,并打印出Ms和Fs的百分比。

这是我到目前为止所提出的:

def percentageGenders(str):
super="M";
print(str.count(super))
sub="F";
(str.count(sub))
total=(str.count(super)) +(str.count(sub));
print (str.count(super))/total
print total

上述代码不会产生预期的结果。寻找建议

0 个答案:

没有答案