python学习2 -使用字符串,,字符串所有标准的额序
python学习2 -使用字符串,,字符串所有标准的额序
字符串
所有标准的额序列操作(索引,分片,乘法,判断成员资格,求长度,最大最小值) 对字符串都适用,但,字符串是不可变的
>>> a = ‘my name is hahaahh‘>>> a[:3] = ‘sdsd‘Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> a[:3] = ‘sdsd‘TypeError: ‘str‘ object does not support item assignment
字符串格式串 --精简版
字符串格式化适用字符串格式化操作符即百分号% 来实现
--格式化字符串 %s
>>> format = ‘hello ,%s,%s enough for you‘>>> values = (‘haha‘,‘zhang‘)>>> print format % valueshello ,haha,zhang enough for you
如果要 格式化 是实数 则用 %f
格式化字符串 --完整版
pass
字符串方法 --列举一些 特别有用的
find -可以在一个较长的字符串中查找子串,返回子串所在位置最左端索引,如果没有找到 返回 -1
>>> a = "zhang is a good boy">>> a.find("is")6>>> a.find("cheng")-1>>> "zhang is a ".find("a")2
join --用来连接序列中的元素(元素 必须都是字符串) ,是 split 的逆方法
>>>seq = ["1","2","3"]>>> sep ="+">>> sep.join(seq)‘1+2+3‘
lower --返回字符串的小写字母版
replace --返回某字符串的所有匹配项均被替换后的字符串
>>> ‘this is a boy‘.replace(‘is‘,‘kk‘)‘thkk kk a boy‘
split --将字符串分割成序列 是 join 的逆方法
>>> ‘1+2+3+4+5‘.split(‘+‘)[‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘]#如果不提供分隔符,则将 空格 作为 分隔符>>> "i am a boy".split()[‘i‘, ‘am‘, ‘a‘, ‘boy‘]
strip --去除两边的空格
translate --可以替换字符串的某些部分,但和 replace 不同的是 他只处理单个字符
python学习2 -使用字符串
相关内容
- 为python2.7安装包 RuntimeError: Python version >= 3.5 req
- \t \n \n\t在python中的用法,,print("\tP
- 深入浅出Python(高清版)PDF,,深入浅出Python
- python count(),,count()描述P
- pythonanywhere笔记,,https://ww
- Python语言及应用--阅读笔记,,大括号的英文是cur
- python的发展史,,python的发展史
- python设计网站,,系统概要说明现在,随
- 什么是Python?Python的设计哲学?,,Python?Pyt
- 【Python】itchat,,错误:http://
评论关闭