Python str方法总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python str方法总结相关的知识,希望对你有一定的参考价值。
1.返回第一个字母大写
1
2
3
4
|
>>>a = ‘shaw‘ >>> b = a.capitalize() >>> print b Shaw |
2.按指定长度填充特定字符
1
2
3
4
5
6
7
|
>>> a = ‘linux‘ >>> print a.center( 7 , ‘h‘ ) hlinuxh >>> print a.center( 8 , ‘h‘ ) hlinuxhh >>> print a.center( 9 , ‘h‘ ) hhlinuxhh |
3.查找某字符串出现的次数
1
2
3
4
5
6
7
8
9
|
>>> a = "this is my dog, i love this dog and it‘s a good dog!" >>> print a.count( ‘dog‘ ) 3 >>> print a.count( ‘dog‘ , 15 ) 2 >>> print a.count( ‘dog‘ , 15 , 30 ) 0 >>> print a.count( ‘dog‘ , 15 , 32 ) 1 |
4.以指定的编码格式解码字符串。默认编码为字符串编码(适合python2中处理中文)
1
2
3
|
b = ‘strid‘ >>> b.decode( ‘utf-8‘ ) u ‘strid‘ |
5.用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置
1
2
3
4
5
6
7
|
>>> shaw = ‘I am shaw,what\‘s your name ?‘ >>> shaw.endswith( ‘?‘ ) True >>> shaw.endswith( ‘w‘ , 7 , 9 ) True >>> shaw.endswith( ‘w‘ , 7 , 8 ) False |
6.把字符串中的 tab 符号(‘\t‘)转为空格,tab 符号(‘\t‘)默认的空格数是8,tabsize -- 指定转换字符串中的 tab 符号(‘\t‘)转为空格的字符数。
1
2
3
4
5
6
7
|
>>> info = ‘today is a good d\tay‘ >>> print info.expandtabs() today is a good d ay >>> print info.expandtabs( 4 ) # 把tab装换成4个空格 today is a good d ay >>> printinfo.expandtabs( 1 ) today is a good d ay # 把tab装换成1个空格 |
7.检测字符串中是否包含子字符串 str ,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串,则返回开始的索引值,否则返回-1。
1
2
3
4
5
6
7
|
>>> a = ‘stivenwang‘ >>> a.find( ‘w‘ ) 6 >>> a.find( ‘w‘ , 9 ) - 1 >>> a.find( ‘w‘ , 9 , 11 ) - 1 |
8.格式换字符串输出(方法与%相似,但可以指定顺序)
margin-bottom: 0px;">9.检测字符串string中是否包含子字符串 str ,如果存在,则返回str在string中的索引值,如果指定beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常(ValueError: substring not found)。 index(...) S.index(sub [,start [,end]])-> int
1
2
3
4
5
6
7
8
|
>>> name = ‘StivenWang‘ >>> fruit = ‘apple‘ >>> print ‘my name is {},I like {}‘ . format (name,fruit) my name is StivenWang,I like apple >>> print ‘my name is {1},I like {0}‘ . format (fruit,name) my name is StivenWang,I like apple >>> print ‘my name is {mingzi},I like{shuiguo}‘ . format (shuiguo = fruit,mingzi = name) my name is StivenWang,I like apple |
1
2
3
4
5
6
7
8
|
>>> str1 = "this is string example....wow!!!" >>> str2 = "exam" >>> print str1.index(str2) 15 >>> print str1.index(str2, 20 ) Traceback (most recent call last): File "<input>" ,line 1 , in <module> ValueError: substringnot found报错 |
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> a = ‘123‘ >>> a.isalnum() True >>> b = ‘shaw‘ >>> b.isalnum() True >>> c = ‘shaw123‘ >>> c.isalnum() True >>> d = ‘th 123‘ >>> d.isalnum() False |
1
2
3
4
5
6
7
8
9
10
11
12
|
>> a = ‘123‘ >>> a.isalpha() False >>> b = ‘123shaw‘ >>> b.isalpha() False >>> c = ‘shaw‘ >>> c.isalpha() True >>> d = ‘sha w‘ >>> d.isalpha() False |
1
2
3
4
5
6
7
8
9
|
>>> a = ‘123‘ >>> a.isdigit() True >>> b = ‘shaw‘ >>> b.isdigit() False >>> c = ‘123shaw‘ >>> c.isdigit() False |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> a = ‘shaw‘ >>> a.islower() True >>> b = ‘123‘ >>> a.islower() True >>> c = ‘123shaw‘ >>> c.islower() True >>> d = ‘SHAW‘ >>> d.islower() False >>> e = ‘123SHAW‘ >>> e.islower() False |
1
2
3
4
5
6
7
8
9
|
>>> a = ‘ ‘ >>> a.isspace() True >>> a = ‘123‘ >>> a.isspace() False >>> a = ‘shaw‘ >>> a.isspace() False |
1
2
3
4
5
6
7
8
9
|
>>> a = ‘Shaw‘ >>> a.istitle() True >>> a = ‘Shaw123‘ >>> a.istitle() True >>> a = ‘123‘ >>> a.istitle() False |
1
2
3
4
5
6
7
8
9
10
11
12
|
>>> a = ‘123‘ >>> a.isupper() False >>> a = ‘Shaw‘ >>> a.isupper() False >>> a = ‘Shaw123‘ >>> a.isupper() False >>> a = ‘SHAW123‘ >>> a.isupper() True |
1
2
3
4
|
>>> a = ‘-‘ >>> b = ‘shaw‘ >>> print a.join(b) s - h - a - w |
1
2
3
4
5
|
>>> s = ‘shaw‘ >>> s.ljust( 10 ) ‘shaw ‘ >>> s.ljust( 10 , ‘8‘ ) ‘shaw888888‘ |
1
2
3
4
5
6
|
>>> s = ‘PYTHON‘ >>> s.lower() ‘python‘ >>> s = ‘PYTHON123‘ >>> s.lower() ‘python123‘ |
1
2
3
4
5
6
|
>>> s = ‘%%%shaw‘ >>> s.lstrip( ‘%‘ ) ‘shaw‘ >>> s = ‘ shaw‘ >>> s.lstrip() ‘shaw‘ |
1
2
3
|
>>> S = ‘are you know:lilin is lowser‘ >>> S.partition( ‘lilin‘ ) ( ‘are you know:‘ , ‘lilin‘ , ‘ is lowser‘ ) |
1
2
3
|
>>> S = ‘shaw‘ >>> S.replace( ‘sh‘ , ‘LI‘ ) ‘LIaw‘ |