6.Strings and Dictionaries
目錄
Strings
1. String syntax
2、Strings are sequences
3、String methods
Strings
Python語言真正發(fā)揮作用的一個(gè)地方是字符串的操作。 本節(jié)將介紹Python的一些內(nèi)置字符串方法和格式化操作。
這種字符串操作模式經(jīng)常出現(xiàn)在數(shù)據(jù)科學(xué)工作中,并且在這種情況下是Python的一大閃光之處。
1. String syntax
在之前的課程中已經(jīng)看過了很多字符串的例子,但回顧一下,Python中的字符串可以使用單引號(hào)或雙引號(hào)來定義。 它們?cè)诠δ苌鲜堑葍r(jià)的。
【1】
x = 'Pluto is a planet' y = "Pluto is a planet" x == yTrue如果您的字符串包含單引號(hào)字符(例如表示撇號(hào)),則使用雙引號(hào)會(huì)很方便。同樣,如果用單引號(hào)將其包裝起來,很容易創(chuàng)建一個(gè)包含雙引號(hào)的字符串:
【2】
print("Pluto's a planet!") print('My dog is named "Pluto"')Pluto's a planet! My dog is named "Pluto"如果我們?cè)趩我?hào)字符串中包含單引號(hào)字母,Python會(huì)報(bào)錯(cuò):
【3】
'Pluto's a planet!'File "<ipython-input-3-16d2b0784e8c>", line 1'Pluto's a planet!'^ SyntaxError: invalid syntax我們可以通過反斜杠“轉(zhuǎn)義”單引號(hào)來解決這個(gè)問題。
[4]
'Pluto\'s a planet!' "Pluto's a planet!"下面這張表格總結(jié)了反斜杠的重要使用例子:
| \' | ' | 'What\'s up?' | What's up? |
| \" | " | "That's \"cool\"" | That's "cool" |
| \\ | \ | "Look, a mountain: /\\" | Look, a mountain: /\ |
| \n | ? | "1\n2 3" | 1 2 3 |
最后一行,\n表示新的一行。
【5】
hello = "hello\nworld" print(hello) hello world此外,Python的字符串三重引用語法讓我們按字面意思包括換行符(即只需在鍵盤上點(diǎn)擊'Enter',而不是使用特殊的'\ n'序列)。 我們已經(jīng)在文檔字符串中看到了這一點(diǎn),但我們可以在任何想要定義字符串的地方使用它們。
【6】
triplequoted_hello = """hello world""" print(triplequoted_hello) triplequoted_hello == hello hello worldTrueprint()函數(shù)會(huì)自動(dòng)添加換行符,除非我們指定關(guān)鍵字參數(shù)end的值而不是默認(rèn)值'\ n':
【7】
print("hello") print("world") print("hello", end='') print("pluto", end='') hello world hellopluto2、Strings are sequences
字符串可以看作是有序字母集合,我們可以按照對(duì)列表的操作對(duì)字符串進(jìn)行操作:
【8】
# Indexing planet = 'Pluto' planet[0] 'P' # Slicing planet[-3:] 'uto'【9】
# How long is this string? len(planet) 5 # Yes, we can even loop over them [char+'! ' for char in planet] ['P! ', 'l! ', 'u! ', 't! ', 'o! ']與列表最大不同的是它們是不可以修改的。
[10]
planet[0] = 'B' # planet.append doesn't work either --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-12-65a7701f8ef6> in <module>() ----> 1 planet[0] = 'B'2 # planet.append doesn't work eitherTypeError: 'str' object does not support item assignment3、String methods
像list一樣,str也有很多有趣的方法,這里有幾個(gè)小例子:
[11]
# ALL CAPS claim = "Pluto is a planet!" claim.upper() 'PLUTO IS A PLANET!'[12]
# all lowercase claim.lower() 'pluto is a planet!'[13]
# Searching for the first index of a substring claim.index('plan') 11[14]
claim.startswith(planet) True[15]
claim.endswith('dwarf planet') False在字符串和列表之間:.split()和.join()
str.split()將字符串拆成更小的字符串,默認(rèn)情況下以空格為界;當(dāng)把字符串拆分成單詞列表時(shí)很有用。
[16]
words = claim.split() words ['Pluto', 'is', 'a', 'planet!']有時(shí)你還想除了空格以外的東西:
[17]
datestr = '1956-01-31' year, month, day = datestr.split('-')str.join()將我們帶到另一個(gè)方向,將一個(gè)字符串列表拼接成一個(gè)長(zhǎng)字符串,使用它作為分隔符調(diào)用的字符串。
[18]
'/'.join([month, day, year]) '01/31/1956'[19]
# Yes, we can put unicode characters right in our string literals :) '總結(jié)
以上是生活随笔為你收集整理的6.Strings and Dictionaries的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kavmm.exe是什么进程 kavmm
- 下一篇: 从零开始学视觉Transformer(4