python學習筆記 (string)

關於python的字符串

Slice with [ start : end : step ]
You can extract a substring (a part of a string) from a string by using a slice. You define a slice by using square brackets, a start offset, an end offset, and an optional step size. Some of these can be omitted. The slice will include characters from offset start to one before end.
• [:] extracts the entire sequence from start to end.
• [ start :] specifies from the start offset to the end.
• [: end ] specifies from the beginning to the end offset minus 1.
• [ start : end ] indicates from the start offset to the end offset minus 1.
• [ start : end : step ] extracts from the start offset to the end offset minus 1, skipping characters by step.
As before, offsets go 0, 1, and so on from the start to the right, and –1,–2, and so forth from the end to the left. If you don’t specify start, the slice uses 0 (the beginning). If you don’t specify end, it uses the end of the string.

其中:
If you don’t specify start, the slice uses 0 (the beginning). If you don’t specify end, it uses the end of the string.說明了默認條件

值得注意的地方:

>>> letters
'abcdefghijklmnopqrstuvwxyz'
>>> len(letters)
26
>>> letters[::-1]
'zyxwvutsrqponmlkjihgfedcba'
>>> letters[0::-1]
'a'
>>> letters[1::-1]
'ba'
>>> letters[1:0:-1]
'b'
>>> letters[1:-1:-1]
''

應是step=-1所帶來的變化

字符串長度:len()

split() :

>>> todos
'get gloves,get mask,give cat vitamins,call ambulance'
>>> list = todos.split(',')
>>> list
['get gloves', 'get mask', 'give cat vitamins', 'call ambulance']
>>> type(list)
<class 'list'>

If you don’t specify a separator, split() uses any sequence of white space characters—newlines, spaces, and tabs.

>>> todos.split()
['get', 'gloves,get', 'mask,give', 'cat', 'vitamins,call', 'ambulance']

join()
部分像是split()的逆操作

>>> list
['get gloves', 'get mask', 'give cat vitamins', 'call ambulance']
>>> "".join(list)
'get glovesget maskgive cat vitaminscall ambulance'
>>> ','.join(list)
'get gloves,get mask,give cat vitamins,call ambulance'

另外,三引號的運用較爲有趣:

>>> poem="""this tripple quote
... make a poem
... a poem"""
>>> poem
'this tripple quote\nmake a poem\na poem'
>>> print(poem)
this tripple quote
make a poem
a poem

附string的各種函數:
len()
split()
join()
startswith()

>>> poem
'this tripple quote\nmake a poem\na poem'
>>> poem.startswith("this ")
True

endswith()

>>> poem
'this tripple quote\nmake a poem\na poem'
>>> poem.endswith("poem")
True

find()

>>> poem
'this tripple quote\nmake a poem\na poem'
>>> poem.find("tripple")
5

rfind()

>>> poem
'this tripple quote\nmake a poem\na poem'
>>> poem.find("poem")
26
>>> poem.rfind("poem")
33

count()

>>> poem
'this tripple quote\nmake a poem\na poem'
>>> poem.count("poem")
2

isalnum():
Are all of the characters in the poem either letters or numbers?

>>> poem2
'this tripple quote\nmake a poem\na poem'
>>> poem2 = " ".join(poem2.split())
>>> poem2
'this tripple quote make a poem a poem'
>>> poem2.isalnum()
False
>>> poem2 = "".join(poem2.split())
>>> poem2
'thistripplequotemakeapoemapoem'
>>> poem2.isalnum()
True

strip()
capitalize()
title()
upper()
lower()
swapcase()
center()
ljust()
rjust()

>>> setup="a duck goes into a bar..."
>>> setup.strip('.')
'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.swapcase()
'A DUCK GOES INTO A BAR...'
>>> setup.capitalize().swapcase()
'a DUCK GOES INTO A BAR...'
>>> setup.center(30)
'  a duck goes into a bar...   '
>>> setup.ljust(30)
'a duck goes into a bar...     '
>>> setup.rjust(30)
'     a duck goes into a bar...'

replace()

>>> setup
'a duck goes into a bar...'
>>> setup.replace('duck','marmoset')
'a marmoset goes into a bar...'
>>> setup.replace('a ', 'a famous ', 100)
'a famous duck goes into a famous bar...'

這裏注意,容易產生如下錯誤:

>>> setup.replace('a', 'a famous', 100)
'a famous duck goes into a famous ba famousr...'
>>> setup.replace('a', 'a famous', 2)//說明數字的用途,前2處位置
'a famous duck goes into a famous bar...'

其他函數參照資料
ppt P39 More String Things

注:可將代碼寫入xxx.py中,用python指令運行

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章