在另一个字符串中找到一个“ hello”字样,其中包含“ hello”字样

时间:2019-04-04 14:48:32

标签: python-3.x

我应该在字符串中找到一个“ hello”单词,该单词是我从输入中给出的。 这是我当前拥有的代码,但是我无法将'hello'与字符列表匹配。

mylist = 'it can be any letter plus hello in it'
for letter in mylist:
    if letter in key:
        mylist2+=letter
print(mylist2)
mylist2 = ['h', 'h', 'e', 'l', 'l', 'l', 'l', 'l', 'o', 'o']

1 个答案:

答案 0 :(得分:0)

这是我认为可以在字符串中找到“ 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))