首页 > 解决方案 > How to make ''string in list'' case-insensitive in python3.8.3

问题描述

print("Say hello in one of these 3 langauges:"+" Hello","Namaste","Salve.")
hello=input()
Language=["English. hello","Hindi. Namaste","Latin. Salve"]
res= [i for i in Language if hello in i]
print(hello+" is hello in"+res)

this code only works when I type in a capital letter but not a lowercase letter.Please help me capfold and capupper do not work

标签: python

解决方案


您可以在将条件的两边都转换为小写后进行比较。

print("Say hello in one of these 3 languages: " + "Hello", "Namaste", "Salve.")

hello = input()
Language = [ "English. hello", "Hindi. Namaste", "Latin. Salve"]

res = [i for i in Language if hello.lower() in i.lower()]

print(hello + " is hello in" + str(res))

推荐阅读