Python How to find if the given inputted strings can be found inside the second inputted strings?
Question:
** I AM NEW TO PYTHON **
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 (I KNOW IT LACKS A LOT FROM THE INSTRUCTION). Hoping someone could teach me what to do. Thank you!
EDIT: I FOUND OUT THAT, MY CODE SHOULD HAVE BEEN:
a1 = str(input("Enter the first string: "))
a2 = str(input("Enter the second string: "))
if a1 in a2:
print(True)
else:
print(False)
But now, how do I make it not case sensitive?
Answers:
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()
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.
** I AM NEW TO PYTHON **
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 (I KNOW IT LACKS A LOT FROM THE INSTRUCTION). Hoping someone could teach me what to do. Thank you!
EDIT: I FOUND OUT THAT, MY CODE SHOULD HAVE BEEN:
a1 = str(input("Enter the first string: "))
a2 = str(input("Enter the second string: "))
if a1 in a2:
print(True)
else:
print(False)
But now, how do I make it not case sensitive?
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()
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.