如何在Python中以不区分大小写的方式比较字符串?
我想使用简单的python代码将常规字符串的比较封装到存储库字符串中。我也想有能力在字典中查找值哈希字符串使用常规的python字符串。
如何在Python中以不区分大小写的方式比较字符串?
我想使用简单的python代码将常规字符串的比较封装到存储库字符串中。我也想有能力在字典中查找值哈希字符串使用常规的python字符串。
当前回答
使用Python 2,在每个字符串或Unicode对象上调用.lower()…
string1.lower() == string2.lower()
...大部分时间都有效,但在@tchrist所描述的情况下确实不起作用。
假设我们有一个名为unicode.txt的文件,其中包含两个字符串Σίσυφος和ΣΊΣΥΦΟΣ。使用Python 2:
>>> utf8_bytes = open("unicode.txt", 'r').read()
>>> print repr(utf8_bytes)
'\xce\xa3\xce\xaf\xcf\x83\xcf\x85\xcf\x86\xce\xbf\xcf\x82\n\xce\xa3\xce\x8a\xce\xa3\xce\xa5\xce\xa6\xce\x9f\xce\xa3\n'
>>> u = utf8_bytes.decode('utf8')
>>> print u
Σίσυφος
ΣΊΣΥΦΟΣ
>>> first, second = u.splitlines()
>>> print first.lower()
σίσυφος
>>> print second.lower()
σίσυφοσ
>>> first.lower() == second.lower()
False
>>> first.upper() == second.upper()
True
Σ字符有两种小写形式ς和Σ, .lower()不能区分大小写进行比较。
然而,从Python 3开始,这三种表单都将解析为ς,并且在两个字符串上调用lower()将正确工作:
>>> s = open('unicode.txt', encoding='utf8').read()
>>> print(s)
Σίσυφος
ΣΊΣΥΦΟΣ
>>> first, second = s.splitlines()
>>> print(first.lower())
σίσυφος
>>> print(second.lower())
σίσυφος
>>> first.lower() == second.lower()
True
>>> first.upper() == second.upper()
True
所以如果你关心边界情况,比如希腊语中的3 sigma,请使用python3。
(作为参考,Python 2.7.3和Python 3.3.0b1显示在上面的解释器打印输出中。)
其他回答
这是另一个正则表达式,我已经学会了爱/恨在过去的一周,所以通常导入(在这种情况下是),反映了我的感受! 做一个正常的函数....征求意见,然后使用....东西= re.compile垃圾邮件(r 'foo * | *’,是的)……re.I(是的。我下面)是相同的IGNORECASE,但你不能犯很多错误写它!
然后你使用正则表达式搜索你的消息,但老实说,这应该是在自己的几页,但重点是,foo或垃圾邮件管道在一起,大小写被忽略。 然后,如果找到其中任何一个,lost_n_found将显示其中一个。如果都不是,则lost_n_found等于None。如果它不等于none,则使用return lost_n_find .lower()返回小写的user_input
这让你更容易匹配任何区分大小写的东西。最后(NCS)代表“没有人认真关心……!”或不区分大小写....无论哪种
如果有人有任何问题,请问我。
import re as yes
def bar_or_spam():
message = raw_input("\nEnter FoO for BaR or SpaM for EgGs (NCS): ")
message_in_coconut = yes.compile(r'foo*|spam*', yes.I)
lost_n_found = message_in_coconut.search(message).group()
if lost_n_found != None:
return lost_n_found.lower()
else:
print ("Make tea not love")
return
whatz_for_breakfast = bar_or_spam()
if whatz_for_breakfast == foo:
print ("BaR")
elif whatz_for_breakfast == spam:
print ("EgGs")
from re import search, IGNORECASE
def is_string_match(word1, word2):
# Case insensitively function that checks if two words are the same
# word1: string
# word2: string | list
# if the word1 is in a list of words
if isinstance(word2, list):
for word in word2:
if search(rf'\b{word1}\b', word, IGNORECASE):
return True
return False
# if the word1 is same as word2
if search(rf'\b{word1}\b', word2, IGNORECASE):
return True
return False
is_match_word = is_string_match("Hello", "hELLO")
True
is_match_word = is_string_match("Hello", ["Bye", "hELLO", "@vagavela"])
True
is_match_word = is_string_match("Hello", "Bye")
False
通常的方法是将字符串大写或小写,以便进行查找和比较。例如:
>>> "hello".upper() == "HELLO".upper()
True
>>>
您可以使用casefold()方法。casefold()方法在比较时忽略案例。
firstString = "Hi EVERYONE"
secondString = "Hi everyone"
if firstString.casefold() == secondString.casefold():
print('The strings are equal.')
else:
print('The strings are not equal.')
输出:
The strings are equal.
使用Python 2,在每个字符串或Unicode对象上调用.lower()…
string1.lower() == string2.lower()
...大部分时间都有效,但在@tchrist所描述的情况下确实不起作用。
假设我们有一个名为unicode.txt的文件,其中包含两个字符串Σίσυφος和ΣΊΣΥΦΟΣ。使用Python 2:
>>> utf8_bytes = open("unicode.txt", 'r').read()
>>> print repr(utf8_bytes)
'\xce\xa3\xce\xaf\xcf\x83\xcf\x85\xcf\x86\xce\xbf\xcf\x82\n\xce\xa3\xce\x8a\xce\xa3\xce\xa5\xce\xa6\xce\x9f\xce\xa3\n'
>>> u = utf8_bytes.decode('utf8')
>>> print u
Σίσυφος
ΣΊΣΥΦΟΣ
>>> first, second = u.splitlines()
>>> print first.lower()
σίσυφος
>>> print second.lower()
σίσυφοσ
>>> first.lower() == second.lower()
False
>>> first.upper() == second.upper()
True
Σ字符有两种小写形式ς和Σ, .lower()不能区分大小写进行比较。
然而,从Python 3开始,这三种表单都将解析为ς,并且在两个字符串上调用lower()将正确工作:
>>> s = open('unicode.txt', encoding='utf8').read()
>>> print(s)
Σίσυφος
ΣΊΣΥΦΟΣ
>>> first, second = s.splitlines()
>>> print(first.lower())
σίσυφος
>>> print(second.lower())
σίσυφος
>>> first.lower() == second.lower()
True
>>> first.upper() == second.upper()
True
所以如果你关心边界情况,比如希腊语中的3 sigma,请使用python3。
(作为参考,Python 2.7.3和Python 3.3.0b1显示在上面的解释器打印输出中。)