import string mystring= 'its master will touch its tail.' myindex = 0 print mystring[3:7]+';' print len(mystring) print mystring.lower() print mystring.upper() print mystring.swapcase() print mystring.capitalize() print string.capwords(mystring) print mystring.title() # upper the first char in each word width = 40 fillchar = '#' print mystring.ljust(width,fillchar) print mystring.rjust(width,fillchar) print mystring.center(width,fillchar) print mystring.zfill(width) start = 0 end = 32 substr = 'its' # mystring.find(substr,[start,[end]]) print mystring.find(substr,start,end) # search forward,return the index of the first char of the first substr,searched in mystring[start:end] # mystring.index(substr, [start, [end]]) print mystring.index(substr,start,end) # similar to s.find(), if there is no such substr, return an error # mystring.rfind(substr, [start, [end]]) print mystring.rfind(substr,start,end) # search backward,return the index of the first char of the first substr,searched in mystring[start:end] # mystring.rindex(substr, [start, [end]])) print mystring.rindex(substr,start,end) # similar to s.rfind(), if there is no such substr, return an error # mystring.count(substr, [start, [end]])) print mystring.count(substr,start,end) # caculate the times substr appears in mystring[start:end]mystring[start:end] oldstr = 'its' newstr = 'his' count = 1 # mystring.replace(oldstr, newstr, [count]) print mystring.replace(substr,newstr,count) # replace the oldstr with newstr, 'count' times mystring = ' hello\t\n' # mystring.strip([chars]) # if chars == null, delete '\n','\r','\t',' ' # else, delete each char in chars print mystring.strip() print mystring.lstrip() mystring = '212341212hello12' print mystring.strip('1234') print mystring.lstrip() tabsize = 2 print mystring.replace(' ','\t').expandtabs(1) mystring = 'what|can|i|do|for|you.' sep = '|' maxsplit = 3 # mystring.split([sep,[maxsplit]]) mylist = mystring.split(sep,maxsplit) print mylist mylist = mystring.rsplit(sep,maxsplit) print mylist mystring = 'hello\ncan you help me\nyes.' # mystring.splitlines([keepends]) print mystring.splitlines(0) print mystring.splitlines(1) mysplit = '|' seq = 'abcd ef' # mystring.join(seq) print mysplit.join(seq) myfrom = 'abcde' myto = '12345' table = string.maketrans(myfrom,myto) mystring = 'aabbabcde' # mystring.translate(table[,deletechars]) print mystring.translate(table,'d') # about codecs modules # encode([encoding,[errors]]) # 'encoding' can be 'gb2312','gbk','gb18030','bz2','zlib','big5','bzse64',etc # 'errors' default be 'strict', meaning 'UnicodeError', it can be 'ignore','replace','xmlcharrefreplace','backslashreplace' as a option mystring = 'what can i do for you' print mystring.encode('gbk') # decode([decoding,[errors]]) print mystring.decode('utf-8') mystring = 'hello, this is Mr.Ding' print mystring.startswith('hello') print mystring.endswith('ding') mystring = 'hehe111' print mystring.isalnum() # if the string consists of only chars and numbers print mystring.isalpha() print mystring.isdigit() print mystring.isspace() # if, only space print mystring.islower() # if, without upper char print mystring.isupper() mystring = 'Hello And You' print mystring.istitle() mystring = '1234' # string.atoi(s[,base]) # base default 10 print string.atoi(mystring,10)+1 print string.atol(mystring,10)+1 mystring = '123.01111' print string.atof(mystring)+0.01
Python string
最新推荐文章于 2024-08-13 18:29:56 发布