python - 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
解决方案
您可以在将条件的两边都转换为小写后进行比较。
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))
推荐阅读
- c# - 如何在 Blazor 的 .razor 文件中使用 Html Helper 方法
- c - 将数据从一个指针复制到另一个指针,但未在 c 中复制完整数据
- git - Git:删除目录并使更改对所有当前分支可见
- office-addins - 从 Sharepoint 读取 Word 文档并在 Word 中显示
- nuxt.js - 如何更新嵌套的 Apollo Cache 3+ 级别?
- python - 安装 scipy 时找不到库 mkl_rt、openblas、lapack - 如何更改库的标志
- javascript - PrimeNG p-table - 无法将项目(行)推入“选择”
- go - 从带有函数的切片中调用函数并获取返回值
- javascript - 反应原生的useEffect中的调用和API?
- python - 在 Google Collab 中重新启动和运行所有命令的命令?