Python3基礎(chǔ)進(jìn)階(二)


3.8·Get Length with len()

The len() function counts characters in a string:

>>> letters = 'abcdefg'

>>> len(letters)

7

3.9·Split with split()

Some functions are specific to strings. To use a string function, type thename of the string, a dot, the name of the function, and anyargumentsthat the function needs:

string . function ( arguments )

You can use the built-in stringsplit()function tobreak a string into a listof smaller stringsbased on someseparator.

>>> letters = 'abc,def,ghi'

>>> letters.split(',')

['abc', 'def', 'ghi']

In the preceding example, the string was calledlettersand the string function was called split(), with the single separator argument ','.If you don’t specify a separator, split() uses any sequence of white space characters—newlines, spaces, and tabs.

>>> letters = 'a b c d,ef gh,i'

>>> letters.split()

['a', 'b', 'c', 'd,ef', 'gh,i']

3.10·Combine with join()

Thejoin()function is the opposite of split(): it collapses alistof stringsintoa singlestring.

>>> crypto_list = ['a', 'b', 'c']

>>> crypto_string = '-'.join(crypto_list)

>>> crypto_string

'a-b-c'

3.11·Playing with Strings

Python has a large set ofstring functions. Let’s explore how the most common of them work.

>>> poem = '''All that doth flow we cannot liquid name

Or else would fire and water be the same;

But that is liquid which is moist and wet

Fire that property can never get.

Then 'tis not cold that doth the fire put out

But 'tis the wet that makes it die, no doubt.'''

Does it start with the letters All?

>>> poem.startswith('All')

True

Does it end withThat's all, folks!?

>>> poem.endswith('That\'s all, folks!')

False

let’s find theoffsetof the first occurrence of the wordthein the poem:

>>> poem.find('the')

73

And the offset of the lastthe:

>>> poem.rfind('the')

214

How many times does the three-letter sequencetheoccur?

>>> poem.count('the')

3

Are all of the characters in the poemeither letters or numbers?

>>> poem.isalnum()

False

Nope, there were some punctuation characters.

3.12·Case and Alignment

In this section, we’ll look at some more uses of thebuilt-in string functions.

Our test string is the following:

>>> setup = '...a ..duck goes into a bar...'

>>> setup.strip('.')

'a ..duck goes into a bar'? ? # Remove . sequences from both ends:

Becausestrings are immutable, none of these examples actually changes the setup string. Each example justtakes the value of setup, does something to it, andreturns the result as a new string.

>>> setup = 'a duck goes into a bar...'

>>> setup.capitalize()

'A duck goes into a bar...'

>>> setup.title()

'A Duck Goes Into A Bar...'

>>> setup.upper()

'A DUCK GOES INTO A BAR...'

>>> setup.lower()

'a duck goes into a bar...'

>>> setup = 'a duck goes into a Bar...'

>>> setup.swapcase()

'A DUCK GOES INTO A bAR...'

Now, we’ll work with somelayout alignment functions.

>>> setup = 'a duck goes into a Bar...'

>>> setup.center(30)

'? a duck goes into a Bar...? '

>>> setup = 'a duck goes into a Bar...'

>>> setup.ljust(30)

'a duck goes into a Bar...? ? '

>>> setup.rjust(30)

'? ? a duck goes into a Bar...'

3.13·Substitute with replace()

You usereplace()for simplesubstring substitution. You give it the old substring, the new one, andhow many instances of the old substring to replace. If you omit this final count argument, it replaces all instances.

>>> setup = 'a b c a b c a'

>>> setup.replace('a', 'd', 2)

'd b c d b c a'

Sometimes, you want to ensure that the substring is a whole word, or the beginning of a word, and so on. In those cases, you needregular expressions.

2017.5.27

Chapter 3. Py Filling: Lists, Tuples, Dictionaries, and Sets

In Chapter 2 we started at the bottom with Python’s basic data types:booleans, integers, floats, and strings. If you think of those as atoms, the data structures in this chapter are like molecules.

1.1·Lists and Tuples

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市晒来,隨后出現(xiàn)的幾起案子乘盼,更是在濱河造成了極大的恐慌哈误,老刑警劉巖牙瓢,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掂榔,死亡現(xiàn)場(chǎng)離奇詭異事示,居然都是意外死亡撮胧,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)讥巡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)掀亩,“玉大人,你說(shuō)我怎么就攤上這事欢顷〔酃鳎” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵抬驴,是天一觀的道長(zhǎng)炼七。 經(jīng)常有香客問(wèn)我,道長(zhǎng)布持,這世上最難降的妖魔是什么豌拙? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮题暖,結(jié)果婚禮上按傅,老公的妹妹穿的比我還像新娘。我一直安慰自己胧卤,他們只是感情好唯绍,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著枝誊,像睡著了一般况芒。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上叶撒,一...
    開(kāi)封第一講書(shū)人閱讀 51,573評(píng)論 1 305
  • 那天绝骚,我揣著相機(jī)與錄音耐版,去河邊找鬼。 笑死皮壁,一個(gè)胖子當(dāng)著我的面吹牛椭更,可吹牛的內(nèi)容都是我干的哪审。 我是一名探鬼主播蛾魄,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼湿滓!你這毒婦竟也來(lái)了滴须?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤叽奥,失蹤者是張志新(化名)和其女友劉穎扔水,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體朝氓,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡魔市,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了赵哲。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片待德。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖枫夺,靈堂內(nèi)的尸體忽然破棺而出将宪,到底是詐尸還是另有隱情,我是刑警寧澤橡庞,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布较坛,位于F島的核電站,受9級(jí)特大地震影響扒最,放射性物質(zhì)發(fā)生泄漏丑勤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一吧趣、第九天 我趴在偏房一處隱蔽的房頂上張望法竞。 院中可真熱鬧,春花似錦再菊、人聲如沸爪喘。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)秉剑。三九已至,卻和暖如春稠诲,著一層夾襖步出監(jiān)牢的瞬間侦鹏,已是汗流浹背诡曙。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留略水,地道東北人价卤。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像渊涝,于是被迫代替她去往敵國(guó)和親慎璧。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

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

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,505評(píng)論 0 23
  • 對(duì)未來(lái)的真正慷慨跨释,是把一切獻(xiàn)給現(xiàn)在胸私。 前兩天,我忽然發(fā)現(xiàn)自己右手上的老年斑不見(jiàn)了鳖谈,而且皮膚變得細(xì)膩岁疼,我暗自...
    公主殿下_閱讀 416評(píng)論 0 1
  • 自己前兩天寫(xiě)了《現(xiàn)在開(kāi)始不算晚》,提醒自己:不要等待缆娃,從現(xiàn)在開(kāi)始做起捷绒。但回過(guò)頭來(lái)再看一遍貓叔的《一切才剛剛開(kāi)始》,...
    云未遠(yuǎn)閱讀 154評(píng)論 0 1
  • 這么多年贯要,經(jīng)歷了太多事暖侨,總是在經(jīng)歷過(guò)以后才會(huì)懂得。 一如感情郭毕,痛過(guò)了它碎,才會(huì)懂得如何保護(hù)自己; - ...
    鳳和先生閱讀 320評(píng)論 0 1