我是python的新手,正在寻找一个程序,要求用户填写字符串(短语或其他字符),然后再填写特定字符。
程序必须返回字符串中字符出现的次数。我需要使用.count函数,但是我无法使代码正常工作。
任何帮助都将受到欢迎!
def search(text,character):
'''This function searches for one character, defined as 2nd argument,
in a given text / string of characters, defined as 1 st argument,
and counts how many times it appears'''
#The 1st argument text here represents the input in which you are looking for
text = input("Type here the text you want: ")
#The 2nd argument character represents the character you are looking for
character = input("Type the character you want to count: ")
text.count('character')
print text.count
有您想要的代码,
我创建了一个函数搜索,它带有两个参数:
然后,我将字符出现的次数存储在字符串文本中并打印出来
def search(text,character):
'''This function searches for one character, defined as 2nd argument,
in a given text / string of characters, defined as 1 st argument,
and counts how many times it appears'''
occ = text.count(character)
print("Occurences:",occ)
#The 1st argument text here represents the input in which you are looking for
text = input("Type here the text you want: ")
#The 2nd argument character represents the character you are looking for
character = input("Type the character you want to count: ")
search(text,character)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句