python学习笔记 (string)

来源:互联网 发布:一号公馆家具淘宝网 编辑:程序博客网 时间:2024/06/16 05:37

关于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 quotemake a poema 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指令运行

原创粉丝点击