1

Here is an example that I would like to print TRUE since Drug is in the string. If disease was in the string, I would like it to print TRUE as well. For all other cases I want to print FALSE

mystring =  "Drug   CID006338583    AC1O3UYX    Stitch  1.515E-3    1.000E0 4.989E-2    5.235E-1    4   63  PTGES,SLC15A1,KLK8,IL7R"
check_if_in_mystring = ['Drug', 'Disease']

if check_if_in_mystring in mystring:
    print("TRUE")
else:
    print("FALSE")
1
  • Hi, Can you please help me in understanding the question better? I am a little confused as to do you want True if both Drug and Disease are in the string or do you want True if either one of them is present? Commented Aug 23, 2018 at 13:49

6 Answers 6

5

Your example is correct you can use 'in' operator on string, just add a loop on 'check_if_in_mystring' to iterate over every element you want to check, like below example.

mystring =  "Drug   CID006338583    AC1O3UYX    Stitch  1.515E-3    1.000E0 4.989E-2    5.235E-1    4   63  PTGES,SLC15A1,KLK8,IL7R"
check_if_in_mystring = ['Drug', 'Disease']

for element in check_if_in_mystring:
    if element in mystring:
        print("TRUE for {}".format(element))
    else:
        print("FALSE for {}".format(element))

output:

TRUE for Drug
FALSE for Disease
Sign up to request clarification or add additional context in comments.

Comments

2

This snippet will detect if any one of check_if_in_mystring is in mystring:

any(word in mystring for word in check_if_in_mystring)

For the exact behavior from the question:

print(str(any(word in mystring for word in check_if_in_mystring)).upper())

1 Comment

short, simple, does what's requested - why downvote sth like this?
0

converting string to list and checking in list can be one solution, as solved below.

mystring =  "Drug   CID006338583    AC1O3UYX    Stitch  1.515E-3    1.000E0 4.989E-2    5.235E-1    4   63  PTGES,SLC15A1,KLK8,IL7R"
check_if_in_mystring = ['Drug', 'Disease']
mystring1 = mystring.split() #convertin string to list, seprated by white space
for item in check_if_in_mystring:
    if item in mystring1:
        print(item)
        print("TRUE")
    else:
        print(item)
        print("FALSE")

Output:printing work and its presence in mystring

Drug
TRUE
Disease
FALSE

1 Comment

There's no need to split mystring into a list.
0

Try this:

>>> my_string = "Drug   CID006338583    AC1O3UYX    Stitch  
1.515E-3    1.000E0 4.989E-2    5.235E-1    4   63  PTGES,SLC15A1,KLK8,IL7R"

>>> print("TRUE" if "Drug" in my_string or "Disease" in my_string else 
"FALSE")

TRUE

>>> my_string = "Some other random string"

>>> print("TRUE" if "Drug" in my_string or "Disease" in my_string else 
"FALSE")

FALSE

>>> my_string = "Has the word Disease"

>>> print("TRUE" if "Drug" in my_string or "Disease" in my_string else 
"FALSE")

TRUE

strings support the in operator, so you can just check if Drug or Disease are in my_string.

Comments

0

Alex Taylor's on the right track, but the any clause can be greatly simplified:

>>> mystring =  "Drug   CID006338583    AC1O3UYX    Stitch  1.515E-3    1.000E0 4.989E-2    5.235E-1    4   63  PTGES,SLC15A1,KLK8,IL7R"
>>> check_if_in_mystring = ['Drug', 'Disease']
>>> any(word in mystring for word in check_if_in_mystring)
True
>>> mystring =  "Poodle   CID006338583    AC1O3UYX    Stitch  1.515E-3    1.000E0 4.989E-2    5.235E-1    4   63  PTGES,SLC15A1,KLK8,IL7R"
>>> any(word in mystring for word in check_if_in_mystring)
False

These approaches loop across the list of words in check_if_in_mystring, and any(...) returns True as soon as the first word in mystring expression is True. If the for word in check_if_in_mystring loop finishes without finding a True value, then any(...) returns False.

1 Comment

I agree this is a better approach. I've changed my answer and made it more specific to the question asked too.
0

You may also use:

mystring.contains(anotherstring)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.