str
1、capitalize:首字符首字母大写,其他字符字母小写
1 | 'chinese' a= |
2、casefold:所有字符字母转小写
1 | 'Chinese' a= |
3、center:返回使用指定值填充字符串到指定长度后的结果
1 | 'chinese' a= |
4、count:查出该变量名某个字符出现的次数
1 | 'chinese' a= |
5、encode:编码
1 | '中国' a = |
6、endswith:判断字符串是否以指定后缀结尾
1 | 'chinese' a= |
7、expandtabs:将字符串中的tab符号(’\t’)转为空格,默认空格数为8
1 | 'I love\tChina!' a = |
8、find:检测字符串中是否包含某个子字符串,有返回开始的索引值,没有返回-1
1 | 'I love China!' a = |
9、format:字符串格式化输出
1 | 'I' a = |
10、format_map:使用映射中的替换返回格式化后的字符串
1 | 'xiaohong' name = |
11、index:检测字符串中是否包含某个子字符串,有返回开始的索引值,没有返回ValueError
1 | 'xiaohong' name = |
12、isalnum:检测字符串是否由字母和数字组成
1 | "runoob2016" a = |
13、isalpha:检测字符串是否由字母组成
1 | 'china' a = |
14、isascii:检测字符串是否由ASCII组成
1 | a.isascii() |
15、isdecimal:检测字符串是否是十进制字符串
1 | 'china' a = |
16、isdigit:检测字符串是否是数字字符串
1 | 'china' a = |
17、isidentifier:判断字符串是否是有效的 Python 标识符,可用来判断变量名是否合法
1 | 'class' a = |
18、islower:检测字符串中字母是否全部为小写字母
1 | 'china' a = |
19、isnumeric:检测字符串中字符是否都为数值型
1 | '123' a = |
20、isprintable:判断字符串中所有字符是否都是可打印字符(in repr())或字符串为空
1 | 'hello\tworld' a = |
21、isspace:判断字符串是否是空白字符串
1 | 'hello world' a = |
22、istitle:检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写
1 | 'I love China' a = |
23、isupper:判断字符串中所有的字母是否都是大写
1 | 'hello world' a = |
24、join:使用某个字符串连接任意字符串
1 | '*'.join(['a','b','c']) |
25、ljust:方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
1 | 'china' a = |
26、lower:将所有字母全部转为小写
1 | 'CHina' a = |
27、lstrip:用于截掉字符串左边的空格或指定字符。
1 | 'china' a = |
28、maketrans
1 | 'abcd' a= |
30、replace:把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次
1 | 'china' a= |
31、rfind:类似于 find()函数,不过是从右边开始查找. 找不到返回-1
1 | 'china hello hello' a = |
32、rindex:类似于 index(),不过是从右边开始 找不到报错
1 | 'china hello hello' a = |
33、rjust:返回一个原字符串右对齐,并使用fillchar(默认空格)填充至长度 width 的新字符串
1 | 'china hello hello' a = |
36、rstrip:删除字符串字符串末尾的空格.
1 | 'china hello hello' a = |
37、rsplit:切片 以什么什么分割
1 | 'china hello hello' a = |
38、splitlines:按照行(‘\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
1 | 'ab\nsda\r' a= |
####
39、startswith:检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。
1 | 'this is hello word' a = |
40、strip:用于移除字符串头尾指定的字符(默认为空格)或字符序列
1 | 'this is hello word isht' a = |
41、swapcase:用于对字符串的大小写字母进行转换。
1 | "dasdaYUIOKJJKmm" a = |
42、title:返回”标题化”的字符串,就是说所有单词的首个字母转化为大写,其余字母均为小写
1 | 'opk iojk hello' a = |
####
43、upper:将字符串中的小写字母转为大写字母
1 | 'opk iojk hello' a = |
44、zfill: 方法返回指定长度的字符串,原字符串右对齐,前面填充0
1 | 'opk iojk hello' a = |
-------------本文结束感谢您的阅读-------------
本文标题:python 内置字符串方法
文章作者:航叔
发布时间:2016年01月10日 - 09:01
原始链接:https://hang123456.gitee.io/title.html
许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。