为了账号安全,请及时绑定邮箱和手机立即绑定

在不同的字符串中找到一个 'hello' 单词,其中包含 'hello'

在不同的字符串中找到一个 'hello' 单词,其中包含 'hello'

跃然一笑 2021-12-29 10:25:18
我应该在一个字符串中找到一个“你好”这个词,这是我从输入中给它的。这是我目前拥有的代码,但我无法将 'hello' 与字符列表匹配。mylist = 'it can be any letter plus hello in it'for letter in mylist:    if letter in key:        mylist2+=letterprint(mylist2)mylist2 = ['h', 'h', 'e', 'l', 'l', 'l', 'l', 'l', 'o', 'o']
查看完整描述

1 回答

?
心有法竹

TA贡献1866条经验 获得超5个赞

这是我认为可以在字符串中找到“hello”字的代码。


firststring = input() #ahhellllloou

to_find = "hello"


def check_string(firststring, to_find):

   c = 0

   for i in firststring:

       #print(i)

       if i == to_find[c]:

           c += 1

       if c == len(to_find):


           return "YES"

   return "NO"


print(check_string(firststring, to_find))


查看完整回答
反对 回复 2021-12-29
  • 1 回答
  • 0 关注
  • 153 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号