Python俱乐部
Python
小课题
京东优惠券
简单比较是用内置函数 cmp() 来比较两个字符串:
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a = "abc" >>> b = "abc" >>> cmp(a, b) 0 >>> c = "def" >>> cmp(a,c) -1 >>>
> >>> import re >>> m = re.search('multi', 'A mUltiCased string', re.IGNORECASE) >>> bool(m) True
在比较前把2个字符串转换成同样大写,用upper()方法,或小写,lower() >>> s = 'A mUltiCased string'.lower() >>> s 'a multicased string' >>> s.find('multi') 2
使用python库difflib可以实现两个字符串的比较,找到相同的部分