[英]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.