- 已关闭**。此问题需要details or clarity。当前不接受答案。
- 想要改进此问题?**添加详细信息并通过editing this post阐明问题。
昨天关门了。
这篇文章是昨天编辑并提交审查的。
Improve this question
如何忽略输入字符串中除第一个单词以外的所有单词?
例如,如果输入为"Hello"
,则程序应打印"input is Hello"
。但是,如果输入为"Hello there"
,则还应打印"input is Hello"
。
我目前正在使用match-case
语句,我已经尝试了几种方法,但我希望有更好的解决方案。
下面是我的代码:
match greeting:
case "hello" + _:
print("Greeting is Hello")
3条答案
按热度按时间xcitsw881#
您可能正在寻找类似这样的内容:
它将匹配hello并收集剩余的项目。
niwlg2el2#
有多种方法可以解决这个问题。第一种方法是拆分字符串并获取第一项,即hello
Second approach use method stars with来自W3学校
nx7onnlm3#
input()
函数用于从用户处获取字符串,然后使用split()
立即将该字符串拆分为单词列表。1.使用
[0]
提取列表中的第一个单词。