Q:如何判斷一個字符串只包含數字字符html
A:一種方法是 a.isdigit()。但這種方法對於包含正負號的數字字符串無效,所以更爲準確的爲:python
try: x = int(aPossibleInt) ... do something with x ... except ValueError: ... do something else ...
這樣更準確一些,適用性也更廣。但若是你已經確信沒有正負號,使用字符串的isdigit()方法則更爲方便。git
簡單比較是用內置函數 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能夠實現兩個字符串的比較,找到相同的部分app
difflib 是python提供的比較序列(string list)差別的模塊。
實現了三個類: less
import difflib from pprint import pprint a = 'pythonclub.org is wonderful' b = 'Pythonclub.org also wonderful' #構造SequenceMatcher類 s = difflib.SequenceMatcher(None, a, b) #獲得相同的block print "s.get_matching_blocks():" pprint(s.get_matching_blocks()) print print "s.get_opcodes():" for tag, i1, i2, j1, j2 in s.get_opcodes(): print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])) #在此實現你的功能
s.get_matching_blocks(): [(1, 1, 14), (16, 17, 1), (17, 19, 10), (27, 29, 0)] s.get_opcodes(): replace a[0:1] (p) b[0:1] (P) equal a[1:15] (ythonclub.org ) b[1:15] (ythonclub.org ) replace a[15:16] (i) b[15:17] (al) equal a[16:17] (s) b[17:18] (s) insert a[17:17] () b[18:19] (o) equal a[17:27] ( wonderful) b[19:29] ( wonderful)
import difflib str1 = "Poor Impulse Control: A Good Babysitter Is Hard To Find" str2 = """ A Good Babysitter Is Hard To Find This is Frederick by Leo Lionni, the first book I picked for myself. I was in kindergarten, I believe, which would be either 1968 or 1969. Frederick has a specific lesson for children about how art is as important in life as bread, but there's a secondary consideration I took away: if we pool our talents our lives are immeasurably better. Curiously, this book is the story of my life, however one interprets those things. I expect Mickey Rooney to show up any time with a barn and a plan for a show, though my mom is not making costumes. My sisters own a toy store with a fantastic selection of imaginative children's books. I try not to open them because I can't close them and put them back. My tantrums are setting a bad example for the kids. Anyway, I mention this because yesterday was Mr. Rogers' 40th anniversary. I appreciate the peaceful gentleman more as time passes, as I play with finger puppets in department meetings, as I eye hollow trees for Lady Elaine Fairchild infestations. Maybe Pete can build me trolley tracks!Labels: To Take Your Heart Away """ s = difflib.SequenceMatcher(None, str1, str2) print len(str1), len(str2) star_a, start_b, length = s.find_longest_match(0, len(str1)-1, 0, len(str2)-1) print star_a, start_b, length print str1[star_a:star_a + length]
輸出結果爲:ide
55 1116 0 1048 1 P 版本爲: 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 Good Babysitter Is Hard To Find.函數
將 str1 於 str2 交換一下, len(str1) > len(str2).
則輸出結果是想獲得的結果。 ui
下面列出了經常使用的python實現的字符串操做this
#strcpy(sStr1,sStr2) sStr1 = 'strcpy' sStr2 = sStr1 sStr1 = 'strcpy2' print sStr2
#strcat(sStr1,sStr2) sStr1 = 'strcat' sStr2 = 'append' sStr1 += sStr2 print sStr1
#strchr(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'r' nPos = sStr1.index(sStr2) print nPos
#strcmp(sStr1,sStr2) sStr1 = 'strchr' sStr2 = 'strch' print cmp(sStr1,sStr2)
#strspn(sStr1,sStr2) sStr1 = '12345678' sStr2 = '456' #sStr1 and chars both in sStr1 and sStr2 print len(sStr1 and sStr2)
#strlen(sStr1) sStr1 = 'strlen' print len(sStr1)
#strlwr(sStr1) sStr1 = 'JCstrlwr' sStr1 = sStr1.upper() print sStr1
#strncat(sStr1,sStr2,n) sStr1 = '12345' sStr2 = 'abcdef' n = 3 sStr1 += sStr2[0:n] print sStr1
#strncmp(sStr1,sStr2,n) sStr1 = '12345' sStr2 = '123bc' n = 3 print cmp(sStr1[0:n],sStr2[0:n])
#strncpy(sStr1,sStr2,n) sStr1 = '' sStr2 = '12345' n = 3 sStr1 = sStr2[0:n] print sStr1
#stricmp(sStr1,sStr2) sStr1 = 'abcefg' sStr2 = 'ABCEFG' print cmp(sStr1.upper(),sStr2.upper())
#strnset(sStr1,ch,n) sStr1 = '12345' ch = 'r' n = 3 sStr1 = n * ch + sStr1[3:] print sStr1
#strpbrk(sStr1,sStr2) sStr1 = 'cekjgdklab' sStr2 = 'gka' nPos = -1 for c in sStr1: if c in sStr2: nPos = sStr1.index(c) break print nPos
#strrev(sStr1) sStr1 = 'abcdefg' sStr1 = sStr1[::-1] print sStr1
#strstr(sStr1,sStr2) sStr1 = 'abcdefg' sStr2 = 'cde' print sStr1.find(sStr2)
#strtok(sStr1,sStr2) sStr1 = 'ab,cde,fgh,ijk' sStr2 = ',' sStr1 = sStr1[sStr1.find(sStr2) + 1:] print sStr1