Python Forum
How can I find a string in sequence (with Booleans)? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can I find a string in sequence (with Booleans)? (/thread-17770.html)



How can I find a string in sequence (with Booleans)? - go127a - Apr-23-2019

I receive a String. For example in here I have : Mystring= ‘alohrefllluqoo’ I am going to find the ‘hello’ in that string by using Booleans. Is it possible?

the final output would be 'YES', cause when we remove extra letter we can see the 'Hello' word in first string.

and if the sequence and the word can not be found, the output will be 'NO'


RE: How can I find a string in sequence (with Booleans)? - ichabod801 - Apr-23-2019

Using booleans (True and False)? No, not possible. Using conditionals that evaluate to True or False (if mystring[index] == char:)? Possible. I would prefer the index method of sequences, or better yet regular expressions.

In any case, what have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.


RE: How can I find a string in sequence (with Booleans)? - go127a - Apr-23-2019

I have wrote it in this way. but some one told me that I can Use Boolean-without any further info and I am wonder how?!!

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))



RE: How can I find a string in sequence (with Booleans)? - ichabod801 - Apr-23-2019

As I said, python tags, not output tags.

If someone said you can do that using booleans, I don't know what they are talking about either. I would ask them to clarify what they mean.

Although, I would have the function return True and False rather than "YES" and "NO". Maybe that's what they were talking about. Then you can just use if check_string(text, to_find): rather than if check_string(text, to_find) == 'YES':.