1.正则表达式,使用IGNORECASE标志>>> import re>>> m = re.search('multi', 'A mUltiCased string', re.IGNORECASE)>>> bool(m)2.在比较前把2个字符串转换成同样大写,用upper()方法,或小写,lower()>>> s = 'A mUltiCased string'.lower()>> print list_of_string #将 字符串 分离开,放入列表中 print '*'*50 def case_insensitive_sort(liststring): listtemp = [(x.lower(),x) for x in liststr 一般我们用strcmp可 比较 两个 字符串 的大小, 比较 方法 为对两个 字符串 从前往后逐个字符相 比较 (按\text{ASCII}ASCII码值大小 比较 ),直到出现不同的字符或遇到'\0'为止。如果全部字符都相同,则认为相同;如果出现不相同的字符,则以第一个不相同的字符的 比较 结果为准(注意:如果某个 字符串 遇到'\0',而另一个 字符串 还未遇到'\0',则前者小于后者)。 但在有些时候,我们 比较 字符串 的大小时,希望 忽略 字母的大小,例如"Hello"和"hello"在 忽略 字母 大小写 时是相等的。请写一个程序,实.. re.search(pattern, mystring, re.IGNORECASE) 其中,re.IGNORECASE是compile的flag参数。默认是识别正则中字母的 大小写 。 注意:IGNORECASE必须是大写,小写无效。 加上该参数之后,可以找出正则中的字母的大写和小写情况。 方法 二: 利用 正则表达式 来进行 大小写 忽略 ,示例如下: re.search(r' text = 'UPPER PYTHON , lower python , Mixed Python ' print(re.findall(' python ', text, flags=re.IGNORECASE)) print(re.sub(' python ', 'PHP', t
from string import punctuation ##导入 python string类自带的标点符号 eg = input("请输入:") s = eg.replace(" ","") a = s.lower() dicts={i:'' for i in punctuation} b=str.maketrans(dicts) #用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是 字符串 ,表示需要转换的字符,第二个参数也是 字符串 表示转换的目标 c=a.translate(b
string1 = "This is a Sample Text" string2 = "this is a sample text" if string1.lower().replace(" ", "") == string2.lower().replace(" ", ""): print("The strings are equal.") else: print("The strings are not equal.") 这样可以得到的结果是 "The strings are equal."