Python基础教程之第3章 使用字符串
@[\\]^_`{|}~' >>> string.printable '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' #3.4.1 find >>> 'With a moo-moo here. and a moo-moo there'.find('moo') 7 >>> title = "Monty Python's Flying Circus" >>> title.find('Monty') 0 >>> title.find('Python') 6 >>> titie.find('Flying') Traceback (most recent call last): File "<pyshell#59>", line 1, in <module> titie.find('Flying') NameError: name 'titie' is not defined >>> title.find('Flying') 15 >>> title.find('Zirquss') -1 >>> subject = '$$$ Get rich now!!! $$$' >>> subject.find('$$$') 0 >>> subject.find('$$$', 1) 20 >>> subject.find('!!!') 16 >>> subject.find('!!!', 0, 16) -1 #3.4.2 join >>> seq = [1,2,3,4,5] >>> sep = '+' >>> sep.join(seq) Traceback (most recent call last): File "<pyshell#69>", line 1, in <module> sep.join(seq) TypeError: sequence item 0: expected string, int found >>> seq = ['1','2','3','4','5'] >>> sep.join(seq) '1+2+3+4+5' >>> dirs = '','usr','bin','env' >>> '/'.join(dirs) '/usr/bin/env' >>> print 'C:' + '\\'.join(dirs) C:\usr\bin\env #3.4.3 lower >>> 'Trondheim Hammer Dance'.lower() 'trondheim hammer dance' >>> if 'Gumby' in ['gumby','smith','jones']: print 'Found it!' >>> name='Gumby' >>> names=['gumby','smith','jones'] >>> if name.lower() in names: print 'Found it!' Found it! #標題轉換 >>> "that's all folks".title() "That'S All Folks" >>> import string >>> string.capwords("that's all, folks") "That's All, Folks" #3.4.4 replace >>> 'This is a test'.replace('is','eez') 'Theez eez a test' #3.4.5 split >>> '1+2+3+4+5'.split('+') ['1', '2', '3', '4', '5'] >>> '/usr/bin/env'.split('/') ['', 'usr', 'bin', 'env'] >>> 'Using the default'.split() ['Using', 'the', 'default'] #3.4.6 strip 相當于Java 中的 String.trim() >>> ' internal whitespace is kept '.strip() 'internal whitespace is kept' >>> names = ['gumby','smith','jones'] >>> name = 'gumby' >>> if name in names: print 'Found it!' Found it! >>> '*** SPAM * for * everyone!!! ***'.strip(' *!') 'SPAM * for * everyone' #3.4.7 translate >>> from string import maketrans >>> table = maketrans('cs', 'kz') >>> len(table) 256 >>> table[97:123] 'abkdefghijklmnopqrztuvwxyz' >>> maketrans('','')[97:123] 'abcdefghijklmnopqrstuvwxyz' >>> table = maketrans('cs','kz') >>> 'this is an incredible test'.translate(table) 'thiz iz an inkredible tezt' >>> 'this is an incredible test'.translate(table,' ') 'thizizaninkredibletezt' #非英語字符串問題 table = maketrans('X','x') word = 'Xxx' print word.translate(table).lower() print u'Xxx'.lower() #小結 #本章介紹了字符串的兩種很重要的使用方式 #字符串格式化: 求模操作符(%)能夠用來將其它值轉換為包含轉換標志的字符串,比如%s. 它還能用來對值進行不同方式的格式化, #包含左右對齊, 設定字段寬度以及精度,添加符號(正負號)或者左填充數字0等. #字符串方法 有些很實用,比方split和join,有些則用得很少,比方istitle或capitalize. #本章的新函數 #string.capwords(s[, sep]) 使用split函數切割字符串s(以sep為分隔符),使用capitalize函數將切割得到的各單詞首字母大寫,而且使用join函數以sep為分隔符 #將各單詞連接起來 #string.maketrans(from,to) 創建用于轉換的轉換表 #接下來學什么 列表, 字符串和字典是Python中最重要的3種數據類型.
代碼清單3-1 字符串格式化演示樣例?
#e3-1 #使用給定的寬度打印格式化后的價格列表width = input('Plese enter width: ')price_width = 10 item_width = width - price_width;#減號(-1)用來左對齊數值 header_format = '%-*s%*s' format = '%-*s%*.2f'print '=' * widthprint header_format % (item_width, 'Item', price_width, 'Price')print '-' * widthprint format % (item_width, 'Apples', price_width, 0.4) print format % (item_width, 'Pears', price_width, 0.5) print format % (item_width, 'Cantaloupes', price_width, 1.92) print format % (item_width, 'Dried Apricots (16 oz.)', price_width, 8) print format % (item_width, 'Prunes (4 lbs.)', price_width, 12)print '=' * width#python e3-1.py #Plese enter width: 35 #=================================== #Item Price #----------------------------------- #Apples 0.40 #Pears 0.50 #Cantaloupes 1.92 #Dried Apricots (16 oz.) 8.00 #Prunes (4 lbs.) 12.00 #===================================
轉載于:https://www.cnblogs.com/brucemengbm/p/6978736.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python基础教程之第3章 使用字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 定义一个Matrix类,实现矩阵的加法和
- 下一篇: [python 学习] requests