Can you make an if statement that accepts any combination of upper and lowercase letters in a specific input string in Python such as "You", or "YUo"?
Asked
Active
Viewed 66 times
0
-
1You can use `lower` or `casefold`: https://docs.python.org/3/library/stdtypes.html#str.casefold – j1-lee Aug 01 '22 at 20:04
-
@j1-lee I think he is also asking for something like an "elastic search" to account for misspelled words also? [Similar to this](https://stackoverflow.com/questions/10018679/python-find-closest-string-from-a-list-to-another-string) – Michael S. Aug 01 '22 at 20:06
-
1Is "YUo" a typo? Like, is it supposed to be "YoU"? Or is the U really supposed to be in a different place? – wjandrea Aug 01 '22 at 20:09
-
1Possible duplicates: [How do I do a case-insensitive string comparison?](/q/319426/4518341), [Checking if two strings contain the same characters, not considering their frequencies](/q/13667113/4518341) – wjandrea Aug 01 '22 at 20:12
-
BTW, welcome to Stack Overflow! Check out the [tour] and [How to ask a good question](/help/how-to-ask). – wjandrea Aug 01 '22 at 20:13
3 Answers
1
You could modify the string so that everything is lowercase like this:
user_input = input("Say something: ")
if user_input.lower() == "you":
print("hi")

ada7ke
- 61
- 1
- 11
0
Any combination can be achieved with set()
input_str = 'YUo' # 'You' 'oYu'
match_str = 'you'
if set(input_str.lower()) == set(match_str):
print('matched')

jabbson
- 4,390
- 1
- 13
- 23
-1
You could use ternary operators to check this like following:
True if False not in [True if (i.lower() in y.lower()) else False for i in x] else False

Aanish Amir Waseem
- 181
- 7
-
These conditional expressions are totally redundant. Either way, it looks like you want to use `any()`/`all()` instead. – wjandrea Aug 01 '22 at 20:14