python 内置字符串方法 | 航叔のFamily

python 内置字符串方法

str

1、capitalize:首字符首字母大写,其他字符字母小写

1
2
3
>>> a='chinese'
>>> a.capitalize()
'Chinese'

2、casefold:所有字符字母转小写

1
2
3
>>> a='Chinese'
>>> a.casefold()
'chinese'

3、center:返回使用指定值填充字符串到指定长度后的结果

1
2
3
>>> a='chinese'
>>> a.center(20,'*')
'******chinese*******'

4、count:查出该变量名某个字符出现的次数

1
2
3
>>> a='chinese'
>>> a.count('e')
2

5、encode:编码

1
2
3
4
5
>>> a = '中国'
>>> a.encode('UTF-8')
b'\xe4\xb8\xad\xe5\x9b\xbd'
>>> a.encode('gbk')
b'\xd6\xd0\xb9\xfa'

6、endswith:判断字符串是否以指定后缀结尾

1
2
3
4
5
>>> a='chinese'
>>> a.endswith('nese')
True
>>> a.endswith('neses')
False

7、expandtabs:将字符串中的tab符号(’\t’)转为空格,默认空格数为8

1
2
3
4
5
>>> a = 'I love\tChina!'
>>> a.expandtabs()
'I love China!'
>>> a.expandtabs(16)
'I love China!'

8、find:检测字符串中是否包含某个子字符串,有返回开始的索引值,没有返回-1

1
2
3
4
5
>>> a = 'I love China!'
>>> a.find('China')
7
>>> a.find('China',10)
-1

9、format:字符串格式化输出

1
2
3
4
5
6
7
8
>>> a = 'I'
>>> b = 'tqq'
>>> c = '{0} am {1}'.format(a,b)
>>> c
'I am tqq'
>>> c = '{} am {}'.format(a,b)
>>> c
'I am tqq'

10、format_map:使用映射中的替换返回格式化后的字符串

1
2
3
4
5
>>> name = 'xiaohong'
>>> n=20
>>> s = '{name} has {n} messages.'
>>> s.format_map(vars())
'xiaohong has 20 messages.'

11、index:检测字符串中是否包含某个子字符串,有返回开始的索引值,没有返回ValueError

1
2
3
4
5
6
7
>>> name = 'xiaohong'
>>> name.index('h')
4
>>> name.index('hi')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

12、isalnum:检测字符串是否由字母和数字组成

1
2
3
4
5
6
>>> a = "runoob2016"
>>> a.isalnum()
True
>>> b = "www.baidu.com"
>>> b.isalnum()
False

13、isalpha:检测字符串是否由字母组成

1
2
3
4
5
6
>>> a = 'china'
>>> a.isalpha()
True
>>> b = 'china 1'
>>> b.isalpha()
False

14、isascii:检测字符串是否由ASCII组成

1
2
3
4
5
>>> a.isascii()
True
>>> b = '中国'
>>> b.isascii()
False

15、isdecimal:检测字符串是否是十进制字符串

1
2
3
4
5
6
7
8
9
>>> a = 'china'
>>> a.isdecimal()
False
>>> b = '123'
>>> b.isdecimal()
True
>>> c = '12.5'
>>> c.isdecimal()
False

16、isdigit:检测字符串是否是数字字符串

1
2
3
4
5
6
7
8
9
>>> a = 'china'
>>> a.isdigit()
False
>>> b = '123'
>>> b.isdigit()
True
>>> c = '12.5'
>>> c.isdigit()
False

17、isidentifier:判断字符串是否是有效的 Python 标识符,可用来判断变量名是否合法

1
2
3
4
5
6
>>> a = 'class'
>>> a.isidentifier()
True
>>> b = '123'
>>> b.isidentifier()
False

18、islower:检测字符串中字母是否全部为小写字母

1
2
3
4
5
6
>>> a = 'china'
>>> a.islower()
True
>>> b = 'China'
>>> b.islower()
False

19、isnumeric:检测字符串中字符是否都为数值型

1
2
3
4
5
6
7
8
9
>>> a = '123'
>>> a.isnumeric()
True
>>> b = '12.5'
>>> b.isnumeric()
False
>>> c = 'china'
>>> c.isnumeric()
False

20、isprintable:判断字符串中所有字符是否都是可打印字符(in repr())或字符串为空

1
2
3
4
5
6
7
8
9
>>> a = 'hello\tworld'
>>> a.isprintable()
False
>>> b = 'hello world'
>>> b.isprintable()
True
>>> c = 'hello\nworld'
>>> c.isprintable()
False

21、isspace:判断字符串是否是空白字符串

1
2
3
4
5
6
>>> a = 'hello world'
>>> a.isspace()
False
>>> b = ' '
>>> b.isspace()
True

22、istitle:检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写

1
2
3
4
5
6
>>> a = 'I love China'
>>> a.istitle()
False
>>> B = 'I Love China'
>>> B.istitle()
True

23、isupper:判断字符串中所有的字母是否都是大写

1
2
3
4
5
6
>>> a = 'hello world'
>>> a.isupper()
False
>>> b = 'HELLO WORLD'
>>> b.isupper()
True

24、join:使用某个字符串连接任意字符串

1
2
>>> '*'.join(['a','b','c'])
'a*b*c'

25、ljust:方法返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。

1
2
3
>>> a = 'china'
>>> a.ljust(20,'*')
'china***************'

26、lower:将所有字母全部转为小写

1
2
3
>>> a ='CHina'
>>> a.lower()
'china'

27、lstrip:用于截掉字符串左边的空格或指定字符。

1
2
3
>>> a ='china'
>>> a.lstrip('c')
'hina'

28、maketrans

1
2
3
4
5
6
>>> a= 'abcd'
>>> b = '1234'
>>> c = str.maketrans(a,b)
>>> d = 'china'
>>> d.translate(c)
'3hin1'

30、replace:把 将字符串中的 str1 替换成 str2,如果 max 指定,则替换不超过 max 次

1
2
3
>>> a= 'china'
>>> a.replace(a,'asda')
'asda'

31、rfind:类似于 find()函数,不过是从右边开始查找. 找不到返回-1

1
2
3
>>> a = 'china hello  hello'
>>> a.rfind('hello')
13

32、rindex:类似于 index(),不过是从右边开始 找不到报错

1
2
3
>>> a = 'china hello  hello'
>>> a.rindex('hello')
13

33、rjust:返回一个原字符串右对齐,并使用fillchar(默认空格)填充至长度 width 的新字符串

1
2
3
>>> a = 'china hello  hello'
>>> a.rjust(20,'*')
'**china hello hello'

36、rstrip:删除字符串字符串末尾的空格.

1
2
3
>>> a = 'china hello  hello'
>>> a.rstrip('hello')
'china hello '

37、rsplit:切片 以什么什么分割

1
2
3
>>> a = 'china hello  hello'
>>> a.rsplit('llo')
['china he', ' he', '']

38、splitlines:按照行(‘\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

1
2
3
4
5
>>> a= 'ab\nsda\r'
>>> a.splitlines()
['ab', 'sda']
>>> a.splitlines(True)
['ab\n', 'sda\r']

####

39、startswith:检查字符串是否是以指定子字符串 substr 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查。

1
2
3
4
5
>>> a = 'this is hello word'
>>> a.startswith('this') # 字符是否以this开头
True
>>> a.startswith('hello',8) # 从第八个字符是否以hello开头
True

40、strip:用于移除字符串头尾指定的字符(默认为空格)或字符序列

1
2
3
>>> a = 'this is hello word isht'
>>> a.strip('this')
' is hello word '

41、swapcase:用于对字符串的大小写字母进行转换。

1
2
3
>>> a = "dasdaYUIOKJJKmm"
>>> a.swapcase()
'DASDAyuiokjjkMM'

42、title:返回”标题化”的字符串,就是说所有单词的首个字母转化为大写,其余字母均为小写

1
2
3
>>> a = 'opk iojk hello'
>>> a.title()
'Opk Iojk Hello'

####

43、upper:将字符串中的小写字母转为大写字母

1
2
3
>>> a = 'opk iojk hello'
>>> a.upper()
'OPK IOJK HELLO'

44、zfill: 方法返回指定长度的字符串,原字符串右对齐,前面填充0

1
2
3
>>> a = 'opk iojk hello'
>>> a.zfill(40)
'00000000000000000000000000opk iojk hello'
-------------本文结束感谢您的阅读-------------

本文标题:python 内置字符串方法

文章作者:航叔

发布时间:2016年01月10日 - 09:01

原始链接:https://hang123456.gitee.io/title.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。