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指令运行
- python学习笔记 (string)
- python 学习笔记之string
- Python学习笔记3-string
- python 系统学习笔记(二)---string
- Python学习笔记--字符串操作string
- python 系统学习笔记(二)---string
- python学习笔记7-- string类型
- python string,Unicode和Qstring学习笔记
- python字符串string笔记
- Python笔记--String
- python笔记-string
- 《Beginning Python From Novice to Professional》学习笔记一:String
- python学习笔记(三)——string
- Python开发学习笔记(9) - list,tuple,string切片
- Python学习之 string
- python学习string
- python学习--string模块
- Python-string类学习
- HDU1722 切蛋糕家族的题
- -未来世界的幸存者- 读后感(现实篇和职业篇)
- 语义分割--Fully Convolutional DenseNets for Semantic Segmentation
- 世界随机迷宫:客户端需求整理
- Emmet:HTML/CSS代码快速编写神器(Sublime)
- python学习笔记 (string)
- NoSQL
- 为什么样本方差(sample variance)的分母是 n-1
- 一致性哈希算法原理
- 声纹识别
- 数据结构编程笔记五:第二章 线性表 静态链表的实现
- ionic回到页面顶部
- JavaScript中的求和(递归)
- Git学习-->如何通过Shell脚本自动定时将Gitlab备份文件复制到远程服务器?