《python基礎(chǔ)教程》讀書(shū)筆記第三章-字符串的使用

?

所有的標(biāo)準(zhǔn)序列操作對(duì)字符串都適用:索引灾锯,分片葬凳,乘法绰垂,判斷成員資格,求長(zhǎng)度火焰,最小值和最大值劲装。


1.字符串直接賦值

>>> website = 'http://www.baidu.com'

>>> website[-3:]

'com'

2.字符串格式化

>>> format = 'hello,%s.%s enough for ya?'

>>> values = ('world','hot')

>>> print(format % values)

hello,world.hot enough for ya?


>>> 'price of book of pythen $%d' %67

'price of book of pythen $67'

>>> 'price of book of pythen in 0x format $%x' %67

'price of book of pythen in 0x format $43'

>>> 'price of book of pythen in 0o format $%o' %67

'price of book of pythen in 0o format $103'

eg.打印購(gòu)物小票程序

w = int(input('please input width:'))

price_w = 10

item_w = w - price_w


header_format = '%-*s%*s'

format? ? ? ? = '%-*s%*.2f'


print('='*w)

print( header_format%(item_w,'Item',price_w,'price'))

print('-'*w)

print(format % (item_w,'Apple',price_w,0.4))

print(format % (item_w,'Pears',price_w,0.5))

print(format % (item_w,'Cantaloupes',price_w,2.98))

print(format % (item_w,'Dried Apricots(16 oz)',price_w,16))

print(format % (item_w,'Prunes(4 1bs)',price_w,17.4))

print(format % (item_w,'Total Sum',price_w,17.4+16+2.98+0.5+0.4))

print('='*w)

運(yùn)行結(jié)果:

please input width:40

========================================

Item? ? ? ? ? ? ? ? ? ? ? ? ? ? ? price

----------------------------------------

Apple? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.40

Pears? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0.50

Cantaloupes? ? ? ? ? ? ? ? ? ? ? ? 2.98

Dried Apricots(16 oz)? ? ? ? ? ? ? 16.00

Prunes(4 1bs)? ? ? ? ? ? ? ? ? ? ? 17.40

Total Sum? ? ? ? ? ? ? ? ? ? ? ? ? 37.28

========================================

3.字符串的方法

find方法

>>> s = "i have 10 $ in my pocket"

>>> s

'i have 10 $ in my pocket'

>>> s.find('poc')

18

>>> s.find('tes')

-1

>>> subject = '$$$ get rich now !!!$$$'

>>> subject.find('$$$')

0

>>> subject.find('$$$',1)

20

>>> subject.find('$$$',1,20)

-1

join方法

>>> dirs = ['','usr','bin','env']

>>> '/'.join(dirs)

'/usr/bin/env'

lower方法

>>> dir = '/USR/LOCAL/BIN'

>>> dir.lower()

'/usr/local/bin'

另外還有islower capitalize swapcase istitle upper isupper等方法,參見(jiàn)書(shū)本附錄昌简。

>>> 'i want to study python'.title()

'I Want To Study Python'

>>> 'i want to study python'.upper()

'I WANT TO STUDY PYTHON'


replace方法

>>> 'this is a test to study replace method'.replace('a','f')

'this is f test to study replfce method'

split方法

>>> '/usr/bin/env'.split('/')

['', 'usr', 'bin', 'env']

strip方法

>>> '? ? this is a test to study strip method? ? '.strip()

'this is a test to study strip method


>>> '*******spam*for*everyone!!!!!*****'.strip('!*')

'spam*for*everyone'

translate方法

本章小結(jié):主要是字符串格式化和字符串方法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末占业,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子江场,更是在濱河造成了極大的恐慌纺酸,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件址否,死亡現(xiàn)場(chǎng)離奇詭異餐蔬,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)佑附,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)樊诺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人音同,你說(shuō)我怎么就攤上這事词爬。” “怎么了权均?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵顿膨,是天一觀的道長(zhǎng)锅锨。 經(jīng)常有香客問(wèn)我,道長(zhǎng)恋沃,這世上最難降的妖魔是什么必搞? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮囊咏,結(jié)果婚禮上恕洲,老公的妹妹穿的比我還像新娘。我一直安慰自己梅割,他們只是感情好霜第,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著户辞,像睡著了一般泌类。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上咆课,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天末誓,我揣著相機(jī)與錄音扯俱,去河邊找鬼书蚪。 笑死,一個(gè)胖子當(dāng)著我的面吹牛迅栅,可吹牛的內(nèi)容都是我干的殊校。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼读存,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼为流!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起让簿,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤敬察,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后尔当,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體莲祸,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年椭迎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了锐帜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡畜号,死狀恐怖缴阎,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情简软,我是刑警寧澤蛮拔,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布述暂,位于F島的核電站,受9級(jí)特大地震影響建炫,放射性物質(zhì)發(fā)生泄漏贸典。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一踱卵、第九天 我趴在偏房一處隱蔽的房頂上張望廊驼。 院中可真熱鬧,春花似錦惋砂、人聲如沸妒挎。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)酝掩。三九已至,卻和暖如春眷柔,著一層夾襖步出監(jiān)牢的瞬間期虾,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工驯嘱, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留镶苞,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓鞠评,卻偏偏與公主長(zhǎng)得像茂蚓,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子剃幌,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容