标签:
1.返回第一个字母大写
|
1
2
3
4
|
>>>a = ‘shaw‘>>> b = a.capitalize()>>> print bShaw |
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.格式换字符串输出(方法与%相似,但可以指定顺序)
|
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‘ |
|
1
2
3
4
5
|
>>> s = ‘lilin is good li lao ban‘>>> s.rfind(‘li‘)14>>> s.rfind(‘li‘,0,8)2 |
|
1
2
3
4
5
6
7
8
9
|
>>> s = ‘my name is shaw‘>>> s.rindex(‘s‘)11>>> s.rindex(‘s‘,5,10)9>>> s.rindex(‘s‘,2,8)Traceback (most recent call last): File "<input>",line 1, in <module>ValueError: substring not found |
|
1
2
3
4
5
6
7
|
>>> s = ‘sch‘>>> s.rjust(20)‘ sch‘>>> s.rjust(20,‘0‘)‘00000000000000000sch‘>>> s.rjust(20,‘H‘)‘HHHHHHHHHHHHHHHHHsch‘ |
|
1
2
3
|
>>> s = ‘shaw\n‘>>> s.rstrip(‘\n‘)‘shaw‘ |
|
1
2
3
4
5
|
>>> s = ‘shaw\nlinux\nmac‘>>> s.split(‘\n‘)[‘shaw‘, ‘linux‘, ‘mac‘]>>> s.split(‘\n‘,1)[‘shaw‘, ‘linux\nmac‘] |
|
1
2
3
4
5
6
|
>>> s = ‘what\‘s your name?\n my name is shaw\n how old areyou?‘>>> s.splitlines()["what‘s your name?", ‘ my name is shaw‘, ‘ how old areyou?‘]>>> s.splitlines(1)["what‘s your name?\n", ‘ my name is shaw\n‘, ‘ how old areyou?‘]>>> s.splitlines(3) |
|
1
2
3
4
5
|
>>> s = ‘my name is shaw‘>>> s.startswith(‘my‘)True>>> s.startswith(‘my‘,10,15)False |
|
1
2
3
|
>>> s = ‘my name is sam‘>>> s.strip(‘m‘)‘y name is sa‘ |
|
1
2
3
4
5
6
|
>>> s = ‘stiven‘>>> s.swapcase()‘STIVEN‘>>> s = ‘SHAW‘>>> s.swapcase()‘shaw‘ |
|
1
2
3
|
>>> s = ‘my name is shaw‘>>> s.title()‘My Name Is Shaw‘ |
|
1
2
3
4
5
6
7
8
9
|
>>> from string import maketranssuchas = maketrans(‘sm‘,‘@$‘)>>> s = ‘this is sam\‘s dog‘>>> s"this is sam‘s dog">>> s.translate(suchas)"thi@ i@ @a$‘@ dog">>> s.translate(suchas,‘dog‘) 去除d,o,g字符"thi@ i@ @a$‘@ " |
|
1
2
3
4
5
6
7
8
9
|
>>> s = ‘sam‘>>> s.upper()‘SAM‘>>> s = ‘23sam‘>>> s.upper()‘23SAM‘>>> s = ‘23s am‘>>> s.upper()‘23S AM‘ |
标签:
原文地址:http://www.cnblogs.com/opsedu/p/5500725.html