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

编程入门 行业动态 更新时间:2024-10-21 15:36:21
本文介绍了如何在Python中进行不区分大小写的字符串比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在Python中进行不区分大小写字符串比较的最好方法是什么?

What's the best way to 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. Much obliged for advice.

推荐答案

假设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)"

更多推荐

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

本文发布于:2023-11-10 16:04:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小写   字符串   如何在   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!