从键盘输入一行字符串,统计字符串中特定字符的个数(特定字符需要从键盘输入),并输出特定字符及个数,输出格式见举例。
说明:
(1)在主函数中输入字符串和特定字符,通过调用统计函数(strStat,要把字符串和特定字符传给函数)返回特定字符的个数,并在主函数中输出。
(2)统统计函数要使用循环控制语句自己统计,不能够使用函数或方法进行统计。
(3)输出数据的格式如下: The number of * is ! (位置为特定字符和个数)
简单方法,代码如下,望采纳,谢谢!
def strStat(st,ch):
# fill your codes after this line
s=0
for c in st:
if c == ch:
s+=1
return s
# end of your codes
# do not modify
if __name__ == "__main__":
st = input("输入字符串:")
ch = input("输入特定字符:")
print(f"The number of {ch} is {strStat(st,ch)}!")