python3 字符串方法(1-15)
1.capitalize() 将字符串的第一个字符改为大写
>>> s='i love cnblog'
>>> s.capitalize()
'I love cnblog'
2.casefold() 将字符串所有字符改为小写
>>> (s.capitalize()).casefold()
'i love cnblog'
3.center(width) 将字符串居中,并用空格将字符串填充至width长度,空格均匀分布在两侧,当width<len(s)时没有效果
>>> s.center(20)
' i love cnblog '
4.count(sub[,start[,end]]) 返回sub在字符串里出现的次数,start,end为可选参数,决定范围
>>> s.count('',5,13)
0
>>> s.count('o',5,13)
1
>>> s.count('o',0,13)
2
5.encode(encoding='utf-8',errors='strict') 以encoding指定的编码格式对字符串进行编码
>>> s.encode(encoding='utf-8',errors='strict')
b'i love cnblog'
>>> s.encode()
b'i love cnblog'
6.endswith(sub[,start[,end]]) 检查字符串是否以sub结尾,是返回True,否返回False,start,end为可选参数,决定范围
>>> s.endswith('!',0,13)
False
>>> s.endswith('g')
True
7.expandtabs([tabsize=8]) 把字符串的tab字符(\t)转化为空格,如不指定tabsize,默认为8个空格
>>> s.expandtabs()
'i love cnblog'
>>> s='\t i love cnblog\t'
>>> s.expandtabs()
' i love cnblog '
这里第一个\t转化为8个空格,第二个tab是在后面加了3个空格,与'cnblog'相加共8个字符,并不是直接加8个空格
8.find(sub[,start[,end]]) 检测sub是否在字符串中,如果在则返回index,否则返回-1,start,end为可选参数,决定范围
>>> s='i love cnblog'
>>> s.find('o')
3
>>> s.find('o',3,13)
3
>>> s.find('o',4,13)
11
>>> s.find('g',0,13)
12
这里返回的是sub的index,同时start,end都是包含的。
9.index(sub[,start[,end]]) 类似find(),不同在于如果sub不在字符串中,返回的不是-1而是异常
>>> s='i love cnblog'
>>> s.index('o')
3
>>> s.index('h')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
s.index('h')
ValueError: substring not found
10.isalnum() 如果字符串至少有一个字符,并且所有字符都是字母或数字则返回True,否则False
>>> s='i love cnblog'#有空格
>>> s.isalnum()
False
>>> s='ilovecnblog'
>>> s.isalnum()
True
>>> s='11ii'
>>> s.isalnum()
True
11.isalpha() 如果字符串至少有一个字符,并且所有字符都是字母则返回True,否则False
>>> s='ilovecnblog'
>>> s.isalpha()
True
12.isdigit() 如果字符串只包含数字则返回True,否则返回False
>>> s=''
>>> s.isdigit()
True
13.isdecimal() 如果字符串只包含十进制数字则返回True,否则返回False
>>> s=''
>>> s.isdecimal()
True
>>> s='ox12'#十六进制
>>> s.isdecimal()
False
>>> s='o123'#八进制
>>> s.isdigit()
False
14.islower() 如果字符中至少包含一个能区分大小写的字符,并且这些字符都是小写则返回True,否则返回Flase
isupper()如果字符中至少包含一个能区分大小写的字符,并且这些字符都是大写则返回True,否则返回Flase
>>> s='ilovecnblog'
>>> s.islower()
True
>>> s='ILOVE'
>>> s.isupper()
True
15.isnumeric() 如果字符串只包含数字字符,则返回True,否则返回False
初一看感觉和isdigit()是一样的,但是:
>>> num=''
>>> num.isdigit()
True
>>> num.isnumeric()
True
>>> num=b''
>>> num.isdigit()
True
>>> num.isnumeric()
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
num.isnumeric()
AttributeError: 'bytes' object has no attribute 'isnumeric'
>>> num='四'#汉字的数字,同样的还有罗马数字等
>>> num.isdigit()
False
>>> num.isnumeric()
True
17.isidentifier() 判断字符串是否包含该语言的保留字
'def'.isidentifier()
Out[3]:
True
'eval'.isidentifier()
Out[4]:
True
18.isprintable() 判断字符串中所有的字符串都是可以通过repr表示成字符串,或者字符串是空的,都返回True,否则返回False
chr(1000000).isprintable()
Out[13]:
False
这里使用一个超出字符编码范围的数字去转化成字符,测试其是否可以打印,显然,答案是不行。
19.isspace() 判断字符串,至少有一个字符的字符串中所有字符是否都是空格,不是则返回False
''.isspace()
Out[14]:
False
' '.isspace()
Out[15]:
True
' a'.isspace()
Out[16]:
False
20.istitle() 判断是否是标题格式,这里理解为首字母大写。
'Author'.istitle()
Out[17]:
True
'aA'.istitle()
Out[18]:
False
'Aa'.istitle()
Out[19]:
True
'AAAa'.istitle()
Out[20]:
False
21.isupper() 判断字符串是否全部是大写
'AAAa'.isupper()
Out[21]:
False
22.join() 返回一个用指定字符串分隔的字,或者是将指定字符加入到另一个字符中。
a = '12345'
','.join(a)
Out[23]:
'1,2,3,4,5'
23.lower() 返回的是指定字符串的拷贝,并转化成小写
'AAA'.lower()
Out[24]:
'aaa'
24.ljust() 可以指定宽度,以及填充字符串,返回的是按宽度,填充字符串格式化后的左对齐的字符串。
b = 'a'.ljust(10)
len(b)
Out[28]:
10
'a'.ljust(10, 'A') # 指定以A填充
Out[30]:
'aAAAAAAAAA'
25.partition:在指定字符串中查找sep,如果找到了返回该字符前面的部分,sep,及后面的部分,
如果没找到则返回sep及两个空字符中,类似于split,但又有不同
'ssaafdaf'.partition('f')
Out[3]:
('ssaa', 'f', 'daf')
26.replace ,用指定字符串替换指定字符串,如果不指定替换次数,仅替换第一个。
'this is a test'.replace('a', 'A')
Out[4]:
'this is A test'
27.rfind(): 返回指定子串的最高索引,如果没找到则返回-1,可以指定要开始替换的起始,结束位置。
'this is a test'.rfind('i')
Out[5]:
5
28.rindex(),与上面的rfind一样,只是如果没找到不是返回-1,而是触发错误
'this is a test'.rindex('g')
Traceback (most recent call last):
File "C:\Program Files\Python35\lib\site-packages\IPython\core\interactiveshell.py", line 2869, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-6-f8a393e6a7d2>", line 1, in <module>
'this is a test'.rindex('g')
ValueError: substring not found
29.rjust();与ljust()相对应
'aa'.ljust(10)
Out[25]:
'aa '
'aa'.rjust(10)
Out[26]:
' aa'
30.rpartition()与partition一样,但是是从右边开始
'this is a test'.rpartition('a')
Out[7]:
('this is ', 'a', ' test')
31.rsplit(),与split作用相同,但是从右侧开始
this is a test'.rsplit(' ')
Out[8]:
['this', 'is', 'a', 'test']
但是讲真,如果不仔细考虑你是不会发现它与split有什么不同的,只有当你指定了最大切割次数时才会有效果。
'this is a test'.split(' ', 1)
Out[10]:
['this', 'is a test']
'this is a test'.rsplit(' ', 1)
Out[11]:
['this is a', 'test']
32.rstrip(), 从右侧移除指定字符
'this is a test'.rstrip('t')
Out[12]:
'this is a tes'
33.split(), 按指定字符串对目标字符串进行切割,可以指定切割次数
'this is a test'.split('i', 1)
Out[13]:
['th', 's is a test']
感觉它与partition的不同在于它返回的结果中移除了指定的字符串
34.splitlines(),返回字符串的行,按换行符切割,如果没指定keepends=True,则会将其从结果中移除
'this is a string\n this is a test'.splitlines()
Out[14]:
['this is a string', ' this is a test'] 'this is a string\n this is a test'.splitlines(keepends=True)
Out[16]:
['this is a string\n', ' this is a test']
35.startswith(),判断字符串是否以某个字符开头
'this is a test'.startswith('t')
Out[18]:
True
# 不一定非得一单个字符
'this is a test'.startswith('this')
Out[19]:
True
36.strip() 移除字符串两侧的指定字符串,默认移除空格,需要注意的是可以指定多个字符
'this is a test'.strip('ts')
Out[20]:
'his is a te'
37.swapcase() 转换大小写
'this is A test'.swapcase()
Out[21]:
'THIS IS a TEST'
38.title(), 标题格式,就是首字母大写,其它字符小写
'this is a test'.title()
Out[22]:
'This Is A Test'
39.upper(),将字符全部转成大写
'this is a test'.upper()
Out[24]:
'THIS IS A TEST'
40.zfill(),这里的z指zero,用0将字符填充到指定长度
'aa'.zfill(10)
Out[25]:
'00000000aa'
41.maketrans(),translate,因为内容比较多,见我另一博客
http://www.cnblogs.com/Andy963/p/7060292.html
python3 字符串方法(1-15)的更多相关文章
- Python3字符串方法
Python字符串方法图示: 1.index() 重点 定义:查找并返回指定str的索引位置,如果没找到则会抛异常(查找的顺序是从左至右)可以指定范围:开始位置和结束位置进行查找. 格式:“字符串内容 ...
- python3字符串
Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...
- [转]python3字符串与文本处理
转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...
- python3字符串与文本处理
每个程序都回涉及到文本处理,如拆分字符串.搜索.替换.词法分析等.许多任务都可以通过内建的字符串方法来轻松解决,但更复杂的操作就需要正则表达式来解决. 1.针对任意多的分隔符拆分字符串 In [1]: ...
- (十四)Python3 字符串格式化
Python3 字符串格式化 字符串的格式化方法分为两种,分别为占位符(%)和format方式.占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越广,format方式使用 ...
- Python3字符串 详解
Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...
- python27期day03:字符串详解:整型、可变数据类型和不可变数据类型、进制转换、索引、切片、步长、字符串方法、进制转换、作业题。
1.%s: a = "我是新力,我喜欢:%s,我钟爱:%s"b = a%("开车","唱跳rap")print(b)2.整型: 整数在Pyt ...
- python字符串方法的简单使用
学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...
- JS字符串方法总结整理
//javascript字符串方法总结 1.String.charAt(n) //取得字符串中的第n个字符 2.String.charCodeAt(n) //取得字符串中第n个字符 ...
随机推荐
- Android Studio 单元测试
测试的种类 按岗位划分 黑盒测试:测试逻辑业务 白盒测试:测试逻辑方法 按测试粒度分 方法测试:function test 单元测试:unit test 集成测试:integration test 系 ...
- LitDB笔记
首先,LitDB存储的不是json,而是bson.这里就有问题了,json是可以无限扩展的,但是bson不行.所有insert的时候需要使用实体类而不是hashtable.但是它又提供了一个叫做Bso ...
- 【转】深入分析 iBATIS 框架之系统架构与映射原理
深入分析 iBATIS 框架之系统架构与映射原理 iBATIS 通过 SQL Map 将 Java 对象映射成 SQL 语句和将结果集再转化成 Java 对象,与其他 ORM 框架相比,既解决了 Ja ...
- c#启动进程
//System.Diagnostics.Process p = new System.Diagnostics.Process(); //p.StartInfo.FileName = @"C ...
- golang rbac框架
在 https://github.com/mikespook/gorbac/tree/v1.0 github上新的版本是开发板,得用这里的老版 demo package main import ( & ...
- AngularJS PhoneCat代码分析
转载自:http://www.tuicool.com/articles/ym6Jfen AngularJS 官方网站提供了一个用于学习的示例项目:PhoneCat.这是一个Web应用,用户可以浏览一些 ...
- UVa 297 Quadtrees(树的递归)
Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...
- setcookie各个参数详解
定义和用法 setcookie() 函数向客户端发送一个 HTTP cookie. cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过 ...
- rails中两种回滚-reversible和revert区别
1 通常迁移内容写在change方法中 ,但是有些迁移内容不能自动通过执行rake:rollback回滚, 所以在迁移文件里要使用 reversible 方法,告诉rails如何回滚例如下面 # co ...
- C#导出带有格式的Excel(列宽,合并单元格,显示边框线,加背景颜色等)
源地址:http://blog.sina.com.cn/s/blog_74f702e60101au55.html 导出excel相关设置:http://blog.csdn.net/wanmingtom ...