如何进行不区分大小写的字符串比较? [英] How do I do a case-insensitive string comparison?

查看:94
本文介绍了如何进行不区分大小写的字符串比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Python中执行不区分大小写的字符串比较?

How can I do case insensitive string comparison in Python?

我想使用非常简单且Pythonic的方法将常规字符串与存储库字符串的比较封装在一起办法。我还希望能够使用常规python字符串在由字符串散列的字典中查找值。

I would like to encapsulate comparison of a regular strings to a repository string using in a very simple and Pythonic way. I also would like to have ability to look up values in a dict hashed by strings using regular python strings.

推荐答案

假定ASCII字符串:

Assuming ASCII strings:

string1 = 'Hello'
string2 = 'hello'

if string1.lower() == string2.lower():
    print("The strings are the same (case insensitive)")
else:
    print("The strings are NOT the same (case insensitive)")

这篇关于如何进行不区分大小写的字符串比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆