Python,strings

返回

>>> line = 'aaa bbb ccc' >>> col1 = line[0:3] >>> col3 = line[8:] >>> col1 'aaa' >>> col3 'ccc' >>> line = 'aaa bbb ccc' >>> cols = line.split() >>> cols ['aaa', 'bbb', 'ccc'] >>> line = 'bob,hacker,40' >>> line.split(',') ['bob', 'hacker', '40'] >>> line = "i'mSPAMaSPAMlumberjack" >>> line.split("SPAM") ["i'm", 'a', 'lumberjack'] >>> S = 'a+b+c+' >>> X = S.replace('+','spam') >>> x Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'x' is not defined >>> X 'aspambspamcspam' >>> import string >>> y = string.replace(S,'+','spam') >>> y 'aspambspamcspam' >>> >>> S = 'spammy' >>> S = S[:3] + 'xx' + S[5:] >>> S 'spaxxy' >>> S = 'spammy' >>> S = S.replace('mm','xx') >>> S 'spaxxy' >>> 'aa$bb$cc$dd'.replace('$','SPAM') 'aaSPAMbbSPAMccSPAMdd' >>> S = 'xxxxSPAMxxxxSPAMxxxx' >>> where = S.find('SPAM') >>> where 4 >>> S = S[:where] + 'EGGS' + S[(where+4):] >>> S 'xxxxEGGSxxxxSPAMxxxx' >>> S = 'xxxxSPAMxxxxSPAMxxxx' >>> S.replace('SPAM', 'EGGS') 'xxxxEGGSxxxxEGGSxxxx' >>> S 'xxxxSPAMxxxxSPAMxxxx' >>> S.replace('SPAM', 'EGGS', 1) 'xxxxEGGSxxxxSPAMxxxx' >>> S = 'spammy' >>> L = list(S) >>> L ['s', 'p', 'a', 'm', 'm', 'y'] >>> L[3] = 'x' >>> L[4] = 'x' >>> L ['s', 'p', 'a', 'x', 'x', 'y'] >>> S = ''.join(L) >>> S 'spaxxy' >>> 'SPAM'.join(['eggs','sausage','ham','toast']) 'eggsSPAMsausageSPAMhamSPAMtoast' >>> >>> exclamation = "Ni" >>> "The knights who say %s!" % exclamation 'The knights who say Ni!' >>> "%d %s %d you" % (1,'spam',4) '1 spam 4 you' >>> "%s -- %s -- %s" % (42, 3.14159, [1,2,3]) '42 -- 3.14159 -- [1, 2, 3]' >>> x = 1234 >>> res = "integers: ...%d...%-6d...%06d" % (x,x,x) >>> res 'integers: ...1234...1234 ...001234' >>> x = 1.23456789 >>> x 1.2345678899999999 >>> '%e | %f | %g' % (x,x,x) '1.234568e+000 | 1.234568 | 1.23457' >>> '%-6.2f | %05.2f | %+06.1f' % (x,x,x) '1.23 | 01.23 | +001.2' >>> "%s" % x,str(x) ('1.23456789', '1.23456789') >>> "%(n)d %(x)s" % {"n":1, "x":"spam"} '1 spam' >>> food = 'spam' >>> age = 40 >>> vars() {'exclamation': 'Ni', '__builtins__': <module '__builtin__' (built-in)>, 'res': 'integers: ...1234...1234 ...001234', 'age': 40, 'food': 'spam', 'x': 1.2345678 899999999, '__name__': '__main__', '__doc__': None} >>> "%(age)d %(food)s" % vars() '40 spam' >>> Indexing and Slicing >>> S = 'spam' >>> S[0], S[-2] ('s', 'a') >>> S[1:3], S[1:], S[:-1] ('pa', 'pam', 'spa') >>> "ys" + 1 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: cannot concatenate 'str' and 'int' objects >>> int("42"), str(42) (42, '42') >>> string.atoi("42"),'42' Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'string' is not defined >>> string.atoi("42"),`42` Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'string' is not defined >>> int("42") + 1 43 >>> "spam" + str(42) 'spam42' >>> str(3.1415), float("1.5") ('3.1415', 1.5) >>> text = "1.234E-10" >>> float(text) 1.2340000000000001e-010 >>> S 'spam' >>> S[0] = "x" Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: object doesn't support item assignment >>> S = S + 'SPAM!' >>> S 'spamSPAM!' >>> S = S[:4] + 'Burger' + S[-1] >>> S 'spamBurger!' >>> 'That is %d %s bird!' % (1, 'dead') 'That is 1 dead bird!' >>> >>> len('abc') 3 >>> 'abc' + 'def' 'abcdef' >>> 'Ni!' * 4 'Ni!Ni!Ni!Ni!' >>> print '------...more...---' ------...more...--- >>> print '_'*80 ________________________________________________________________________________ >>> print '------ ...more... ---' ------ ...more... --- >>> print '-'*80 -------------------------------------------------------------------------------- >>> myjob = "hacker" >>> for c in myjob: print c, ... h a c k e r >>> "k" in myjob True >>> "z" in myjob False >>> >>> path = r'C:\new\text.dat' >>> path 'C:\\new\\text.dat' >>> print path C:\new\text.dat >>> len(path) 15 >>> Triple quotes code multiline block strings >>> mantra = """Always look ... on the bright ... side of life.""" >>> mantra 'Always look\non the bright\nside of life.' >>> >>> u'spam' u'spam' >>> 'ni' + u'spam' u'nispam' >>> str(u'spam') 'spam' >>> unicode('spam') u'spam' >>> u'ab\x20cd' u'ab cd' >>> u'ab\u0020cd' u'ab cd' >>> u'ab\U00000020cd' u'ab cd' Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 'shrubbery',"shrubbery" ('shrubbery', 'shrubbery') >>> 'knight"s',"knight's" ('knight"s', "knight's") >>> title = "Meaning " 'of' " Life" >>> title 'Meaning of Life' >>> 'knight\'s',"knight\"" ("knight's", 'knight"') >>> 'knight\'s',"knight\"s" ("knight's", 'knight"s') >>> s = 'a\nb\tc' >>> print s a b c >>> len(s) 5 >>> s = 'a\0b\0c' >>> s 'a\x00b\x00c' >>> len(s) 5 >>> s = '\001\002\x03' >>> s '\x01\x02\x03' >>> len(s) 3 >>> x = "C:\py\code" >>> x 'C:\\py\\code' >>> len(x) 10 >>> c:\python>python Python 2.3.5 (#62, Feb 8 2005, 16:23:02) [MSC v.1200 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> s1 = '' >>> print s1 >>> s2 = "span's" >>> print s2 span's >>> s3 = r'\temp\spam' >>> print s3 \temp\spam >>> s4 = u'spam' >>> print s4 spam >>> s4 = u'人' >>> print s4 ?l >>> myfile = open('myfile','w') >>> myfile.write('s4\n') >>> myfile.close() >>> myfile = open('myfile1.txt','w') >>> myfile.write(s4) Traceback (most recent call last): File "<stdin>", line 1, in ? UnicodeEncodeError: 'ascii' codec can't encode character u'\x90' in position 0: ordinal not in range(128) >>>
返回