我需要计算用户在python中输入的数据

时间:2014-01-17 15:06:06

标签: python count raw-input

我是python的新手,我遇到了一个特定的问题。我需要计算用户将在python中输入的字符或单词。所以基本上我必须计算用户输入的任何内容'请输入一个字符串,然后输入一个子字符串'。我添加了“h = h.lower”,因为我们必须确保字符串是小写的。到目前为止我所拥有的是:

def highlight(): 
        h = h.lower()
        print (raw_input("Please enter a string: "))
        print (raw_input("Please enter a substring: "))
        print("There were",  "occurrences of" +str(raw_input)) 

1 个答案:

答案 0 :(得分:1)

你的问题不够明确,如果你在谈论计算字符串中的特定字符然后使用,

str.count(sub [,start [,end]])

返回[start,end]范围内子串sub的非重叠次数。可选参数start和end被解释为切片表示法。

word = 'elephant' 
word.count('e') 
#Gives you 2