简体   繁体   English

如何正确显示特定单词在用户给定输入中出现的次数?

[英]How to correctly display the number of times a particular word has appeared in the user given input?

I am just starting python so I am working out exercises.我刚刚开始使用python,所以我正在做练习。 I came across this one.我遇到了这个。

Question: Given a string of n words, help Alex to find out how many times his name appears in the string.问题:给定一个包含 n 个单词的字符串,帮助 Alex 找出他的名字在字符串中出现的次数。

Constraint: 1 <= n <= 200约束:1 <= n <= 200

Sample input: Hi Alex WelcomeAlex Bye Alex.示例输入:嗨亚历克斯欢迎亚历克斯再见亚历克斯。

Sample output : 3样本输出:3

My code for this:我的代码:

string =input("Enter input")
print(string.count(string)) 

But this is not giving required output.但这并没有给出所需的输出。 How can I change this?我怎样才能改变这个? Please help.请帮忙。 Thanks.谢谢。

Austin is correct.奥斯汀是对的。

string =input("Enter input")
print(string.count('Alex')) 

This should give you what you wanted.这应该给你你想要的。 Thank You谢谢你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算相同数字在python列表中出现的次数? - How to count the number of times same number has appeared in a list in python? Elasticsearch:我想计算特定值在特定字段中出现的次数 - Elasticsearch: I want to count how many times a particular value has appeared in particular field python计数日期出现的次数 - python count number of times the date has appeared 计算特定字符串在给定单词中出现的次数 - Count the number of times a particular string appears in a given word 如何确定单词在输入中出现的次数 - How to determine the number of times a word appears in an input 如果单词在熊猫的特定列中,如何获得一个片段的次数? - How to get the number of times a piece if word is inside a particular column in pandas? 如何连续检查列表中的数字是否发生了特定次数 - How to check if a number has occurred a particular number of times in a list consecutively 如何找到一个单词在给定字符串中连续重复的最大次数? - How to find the largest number of times a word is repeated consecutively in a given string? 在 Python 中使用 Pandas,如果单词已经出现 3 次,则删除数据框中的行 - Using Pandas with Python, delete row in data frame if word has appeared three times already Pandas 如果列值根据值出现超过一定次数,则删除行 - Pandas drop row if column value has appeared more than some number of times depending on the value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM