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

标签 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)) 

最佳答案

你的问题不够清楚,如果你正在谈论计算字符串中的特定字符,那么使用,

str.count(sub[, 开始[, 结束]])

返回子字符串 sub 在范围 [start, end] 中不重叠出现的次数。可选参数 start 和 end 被解释为切片符号。

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

关于python - 我需要统计用户在 python 中输入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21189101/

相关文章:

Python 使用错误的编码

python - 如何将这个元组的元组转换为其元素的计数?

android - Facebook 返回错误的帖子评论数

Python if 表达式无法正确处理原始输入

python - 如何使用原始输入来定义变量

c++ - 在 Windows 上模拟原始输入

python - 使用 dedupe python 时资源使用率低

python - 根据字典值计算 numpy 数组中向量的总和

python - 加号不携带

python - 通过 python 中的 namedtuple csv 循环跟踪进度