-2

I would like to know how to write a function that returns True if the first string, regardless of position, can be found within the second string by using two strings taken from user input. Also by writing the code, it should not be case sensitive; by using islower() or isupper().

Example Outputs:

1st String: lol

2nd String: Hilol

True

1st String: IDK

2nd String: whatidk

True

My code:

a1 = str(input("first string: "))
a2 = str(input("second string: "))

if a2 in a1: 
    print(True)
else:
    print(False)

It outputs:

1st String: lol

2nd String: lol

True

1st String: lol

2nd String: HIlol

False #This should be true, IDK why it is false.

I only came this far with my code. Hoping someone could teach me what to do.

wovano
  • 4,543
  • 5
  • 22
  • 49
Nemethste
  • 5
  • 2

2 Answers2

1

Is this what you're looking for?

string_1 = input("first string: ")
string_2 = input("second string: ")

if string_1.lower() in string_2.lower(): 
    print(True)
else:
    print(False)

A "function" would be:

def check_occuring(substring, string):
    if substring.lower() in string.lower(): 
        return True
    else:
        return False

string_1 = input("first string: ")
string_2 = input("second string: ")
print(check_occuring(string_1, string_2)) 

Please note that you can also just print or return substring.lower() in string.lower()

Nineteendo
  • 882
  • 3
  • 18
0

If you want the Program to be case sensitive, just make everything lowercase

substring = input("substring: ").lower()
text = input("text: ").lower()

To check if a substring can be found in another string can be done by using the keyword in

>>> "lol" in "HIlol"
True

maybe check your inputs here. The whole program would be

substring = input("substring: ").lower()
text = input("text: ").lower()

print(substring in text)

note: <str> in <str> gives you a boolean so you can use it in if conditions. In this case you can just print the boolean directly.

Hellow2
  • 33
  • 8