标签:下标 enc color dex res 标题 com represent util
官方帮助文档地址:
https://docs.python.org/3.7/library/stdtypes.html#text-sequence-type-str
1. S.capitalize() -> str
将字符串首字母转换为大写 ,其余不变,返回字符串
str = ‘fdffdj‘
str.capitalize()
‘Fdffdj‘
2. S.casefold() -> str
字符串转换为小写,返回字符串
str = ‘FjDkfd‘
str.casefold()
‘fjdkfd‘
3. S.center(width[, fillchar]) -> str
指定长度填充,字符串居中,其余默认用空格填充,也可指定填充符
3.1 默认空格填充
str = ‘fly‘
str.center(20)
‘ fly ‘
3.2 指定填充符
str.center(20,‘-‘)
‘--------fly---------‘
4. S.count(sub[, start[, end]]) -> int
返回字符串sub在字符串s中的个数,start,end为指定查找的开始位置和结束位置,前开后闭区间
str = ‘who i am,i am fly‘
str.count(‘i‘)
2
str.count(‘i‘,10)
0
str.count(‘i‘,5)
1
5. S.encode(encoding=‘utf-8‘, errors=‘strict‘) -> bytes
以指定的编码格式解码字符串。默认编码为字符串编码(适合python2中处理中文)
6.S.endswith(suffix[, start[, end]]) -> bool
判断字符串是否以指定字符结尾,如果是返回True,反之,返回False
>>> str = ‘Good‘
>>> str.endswith(‘r‘)
False
>>> str.endswith(‘d‘)
True
>>> str.endswith(‘D‘)
False
7.S.find(sub[, start[, end]]) -> int
检测s字符串中是否包含子字符串sub,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串,则返回开始的索引值(下标,从0开始),否则返回-1。
>>> str = ‘lilei is a good baby‘ >>> str.find(‘o‘) 12 >>> str.find(‘i‘) 1 >>> str.find(‘j‘) -1 >>> str.find(‘i‘,2,10) 4 >>> str.find(‘oo‘) 12
8.S.rfind(sub[, start[, end]])
find()
rfind()
类似,不同的是如果找不到,find返回最小index值,r返回最大index值。>>> str = ‘maliya is a good girl‘
>>> str.rfind(‘a‘)
10
>>> str.rfind(‘oo‘)
13
>>> str.rfind(‘s‘)
8
>>> str.rfind(‘z‘)
-1
9.S.index(sub[, start[, end]]) -> int
检测s字符串中是否包含子字符串sub,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串,则返回开始的索引值(下标,从0开始),否则报
ValueError。
>>> str = ‘maliya is a good girl‘
>>> str.index(‘g‘)
12
>>> str.index(‘a‘)
1
>>> str.index(‘oo‘)
13
>>> str.index(‘z‘)
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: substring not found
10. S.rindex(sub[, start[, end]]) -> int
index()
rindex()
类似,区别是index()
返回最小index值,rindex()
返回最大index值。find()/rfind()与index()/rindex()的区别是,find找不到返回-1,index报valueerror
>>> str = ‘maliya is a good girl‘
>>> str.rindex(‘g‘)
17
>>> str.rindex(‘a‘)
10
>>> str.rindex(‘oo‘)
13
>>> str.rindex(‘z‘)
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: substring not found
11.S.format(*args, **kwargs) -> str
[[fill]align][sign][#][0][width][,][.precision][type]
常用格式化:
tpl
=
"i am {}, age {}, {}"
.
format
(
"seven"
,
18
,
‘alex‘
)
tpl
=
"i am {}, age {}, {}"
.
format
(
*
[
"seven"
,
18
,
‘alex‘
])
tpl
=
"i am {0}, age {1}, really {0}"
.
format
(
"seven"
,
18
)
tpl
=
"i am {0}, age {1}, really {0}"
.
format
(
*
[
"seven"
,
18
])
tpl
=
"i am {name}, age {age}, really {name}"
.
format
(name
=
"seven"
, age
=
18
)
tpl
=
"i am {name}, age {age}, really {name}"
.
format
(
*
*
{
"name"
:
"seven"
,
"age"
:
18
})
tpl
=
"i am {0[0]}, age {0[1]}, really {0[2]}"
.
format
([
1
,
2
,
3
], [
11
,
22
,
33
])
tpl
=
"i am {:s}, age {:d}, money {:f}"
.
format
(
"seven"
,
18
,
88888.1
)
tpl
=
"i am {:s}, age {:d}"
.
format
(
*
[
"seven"
,
18
])
tpl
=
"i am {name:s}, age {age:d}"
.
format
(name
=
"seven"
, age
=
18
)
tpl
=
"i am {name:s}, age {age:d}"
.
format
(
*
*
{
"name"
:
"seven"
,
"age"
:
18
})
tpl
=
"numbers: {:b},{:o},{:d},{:x},{:X}, {:%}"
.
format
(
15
,
15
,
15
,
15
,
15
,
15.87623
,
2
)
tpl
=
"numbers: {:b},{:o},{:d},{:x},{:X}, {:%}"
.
format
(
15
,
15
,
15
,
15
,
15
,
15.87623
,
2
)
tpl
=
"numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}"
.
format
(
15
)
tpl
=
"numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}"
.
format
(num
=
15
)
>>> str = ‘who are you‘
>>> str.index(‘you‘)
8
>>> str.index(‘yod‘)
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: substring not found
13.str.
isalnum
() --> Bool (True or False)
判断字符串String是否由字符串或数字组成,并且至少有一个字符(不为空)简而言之:只要 c.isalpha()
, c.isdecimal()
, c.isdigit()
, c.isnumeric()
中任意一个为真,则 c.isalnum()
为真。
>>> ‘fhjdfh‘.isalnum() True >>> ‘fhjdfh4545‘.isalnum() True >>> ‘4545‘.isalnum() True >>> ‘_4545‘.isalnum() False
>>> ‘‘.isalnum()
False
14.str.
isalpha
() -->Bool (True or False)
判断字符串String是否只由字母组成,并且至少有一个字符(不为空)
>>> ‘love‘.isalpha()
True
>>> ‘love12‘.isalpha()
False
>>> ‘‘.isalpha()
False
>>> ‘%‘.isalpha()
False
>>> ‘4445‘.isdecimal()
True
>>> ‘4445‘.isdigit()
True
>>> ‘4445‘.isnumeric()
True
>>> ‘②‘.isdecimal()
False
>>> ‘②‘.isdigit()
True
>>> ‘②‘.isnumeric()
True
>>> ‘二‘.isdecimal()
False
>>> ‘二‘.isdigit()
False
>>> ‘二‘.isnumeric()
True
>>> ‘class‘.isidentifier()
True
>>> ‘def‘.isidentifier()
True
>>> ‘if‘.isidentifier()
True
>>> ‘‘.isidentifier()
False
>>> ‘89‘.isidentifier()
False
>>> ‘af 12‘.isidentifier()
False
>>> ‘美男‘.isidentifier()
True
17.S.islower() -> bool
判断所有的字母是否全是小写并且至少有一个字母,返回真,否则返回假
注意只对字符串中的字母进行判断
>>> ‘fhjd‘.islower()
True
>>> ‘Dhjd‘.islower()
False
>>> ‘‘.islower()
False
>>> ‘小‘.islower()
False
18.S.isupper() -> bool
判断所有的字符是否全是大写并且至少有一个字母,返回真,否则返回假
注意只对字符串中的字母进行判断
>>> ‘HAIYOUSHEI‘.isupper()
True
>>> ‘HAiYOUSHEI‘.isupper()
False
>>> ‘DD一‘.isupper()
True
>>> ‘‘.isupper()
False
>>> ‘545‘.isupper()
False
>>> ‘DD55‘.isupper()
True
判断字符串中所有字符是否都是可打印字符(in repr())或字符串为空。
Unicode字符集中“Other” “Separator”类别的字符为不可打印的字符(但不包括ASCII码中的空格(0x20))。可用于判断转义字符。
ASCII码中第0~32号及第127号是控制字符;第33~126号是可打印字符,其中第48~57号为0~9十个阿拉伯数字;65~90号为26个大写英文字母,97~122号为26个小写英文字母。
>>> ‘enen‘.isprintable()
True
>>> ‘~‘.isprintable()
True
>>> ‘\n‘.isprintable()
False
>>> ‘\r‘.isprintable()
False
>>> ‘\t‘.isprintable()
False
>>> ‘ ‘.isspace()
True
>>> ‘‘.isspace()
False
>>> ‘\t\n\r‘.isspace()
True
>>> ‘\t\n\rjfdk‘.isspace()
False
>>> ‘ jfdk‘.isspace()
False
>>> ‘Whaareyou‘.istitle()
True
>>> ‘whaareyou‘.istitle()
False
>>> ‘Whaareyou22‘.istitle()
True
22.S.join(iterable) -> str
>>> ‘~‘.join(‘who are you‘)
‘w~h~o~ ~a~r~e~ ~y~o~u‘
>>> ‘~‘.join([‘who‘,‘are‘,‘you‘])
‘who~are~you‘
>>> ‘ ‘.join([‘who‘,‘are‘,‘you‘])
‘who are you‘
>>> ‘ ‘.join((‘who‘,‘are‘,‘you‘))
‘who are you‘
>>> ‘--‘.join([2019,01,01])
File "<input>", line 1
‘--‘.join([2019,01,01])
^
SyntaxError: invalid token
>>> ‘--‘.join([2019,1,1])
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: sequence item 0: expected str instance, int found
>>> ‘--‘.join(‘ ‘)
‘ ‘
>>> ‘-‘.join({‘name‘:‘who‘,‘do‘:‘eat‘})
‘name-do‘
width -- 指定填充指定字符后新字符串的总长度.
fillchar– 要填充的字符,默认为空格。
>>> ‘haha‘.ljust(10)
‘haha ‘
>>> ‘haha‘.rjust(10)
‘ haha‘
>>> ‘haha‘.rjust(10,‘~‘)
‘~~~~~~haha‘
>>> ‘haha‘.ljust(10,‘~‘)
‘haha~~~~~~‘
>>> ‘haha‘.ljust(2,‘~‘)
‘haha‘
ASCII
编码的字母有效。>>> ‘DFFjdfj‘.lower()
‘dffjdfj‘
>>> ‘jdfj‘.lower()
‘jdfj‘
>>> ‘DFD33‘.lower()
‘dfd33‘
ASCII
编码的字母有效。>>> ‘enha‘.upper()
‘ENHA‘
>>> ‘DDenha‘.upper()
‘DDENHA‘
>>> ‘DDenha874‘.upper()
‘DDENHA874‘
26.S.lstrip([chars]) -> str
S.rstrip([chars]) -> str
S.strip([chars]) -> str
>>> ‘ who ‘.lstrip()
‘who ‘
>>> ‘ who ‘.rstrip()
‘ who‘
>>> ‘ who ‘.strip()
‘who‘
>>> ‘ w ho ‘.strip()
‘w ho‘
>>> ‘~~who--‘.strip(‘~‘)
‘who--‘
>>> ‘~~who--‘.strip(‘-‘)
‘~~who‘
>>> ‘\nwho‘.lstrip(‘-‘)
‘\nwho‘
>>> ‘\nwho‘.lstrip(‘\n‘)
‘who‘
>>> ‘who\n‘.rstrip(‘\n‘)
‘who‘
27.S.replace(old, new[, count]) -> str
把字符串中指定的旧子字符串替换成指定的新子字符串,如果指定 count 可选参数则替换指定的次数,默认全部替换。
>>> ‘what are you doing, i am walking ‘.replace(‘walking‘,‘eating‘)
‘what are you doing, i am eating ‘
>>> ‘what are you doing, i am walking and walking‘.replace(‘walking‘,‘studing‘,1)
‘what are you doing, i am studing and walking‘
>>> ‘what are you doing, i am walking and walking‘.replace(‘walking‘,‘studing‘,2)
‘what are you doing, i am studing and studing‘
>>> ‘what are you doing, i am walking \n and walking\n‘.replace(‘\n‘,‘ ‘,2)
‘what are you doing, i am walking and walking ‘
>>> ‘what are you doing, i am walking and walking‘.replace(‘--‘,‘~~‘,2)
‘what are you doing, i am walking and walking‘
28. S.partition(sep) -> (head, sep, tail)
S.rpartition(sep) -> (head, sep, tail)
根据指定的分隔符将字符串进行分割(返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串)
S.partition(sep) 如果字符串中没有指定分隔符,将返回整个字符串作为第一个,分割的本身以及尾部为空
S.rpartition(sep)如果字符串中没有指定分隔符,将返回整个字符串作为第三个,分割的本身以及头部为空
>>> ‘who are you‘.rpartition(‘are‘)
(‘who ‘, ‘are‘, ‘ you‘)
>>> ‘who are you‘.rpartition(‘r‘)
(‘who a‘, ‘r‘, ‘e you‘)
>>> ‘who are you‘.partition(‘r‘)
(‘who a‘, ‘r‘, ‘e you‘)
>>> ‘who arre you‘.partition(‘r‘)
(‘who a‘, ‘r‘, ‘re you‘)
>>> ‘who arre you‘.rpartition(‘r‘)
(‘who ar‘, ‘r‘, ‘e you‘)
>>> ‘who arre you‘.rpartition(‘s‘)
(‘‘, ‘‘, ‘who arre you‘)
>>> ‘who arre you‘.partition(‘s‘)
(‘who arre you‘, ‘‘, ‘‘)
29.S.split(sep=None, maxsplit=-1) -> list of strings
如果没有字符串中没有找到指定的分隔符,则返回整个字符串的列表
如果指定的分割符在字符串首/尾,则从第二个分割符开始
>>> str = ‘maliya is a good girl‘
>>> str.split()
[‘maliya‘, ‘is‘, ‘a‘, ‘good‘, ‘girl‘]
>>> str = ‘maliya,is,a,good,girl‘
>>> str.split(‘,‘)
[‘maliya‘, ‘is‘, ‘a‘, ‘good‘, ‘girl‘]
>>> str = ‘maliyais,a,good,girl‘
>>> str.split(‘,‘)
[‘maliyais‘, ‘a‘, ‘good‘, ‘girl‘]
>>> str.split(‘,‘,2)
[‘maliyais‘, ‘a‘, ‘good,girl‘]
>>> str.split(‘,‘,1)
[‘maliyais‘, ‘a,good,girl‘]
>>> str.split(‘-‘)
[‘maliyais,a,good,girl‘]
>>> str = ‘ maliyais a good girl ‘
>>> str.split(‘‘)
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: empty separator
>>> str.split()
[‘maliyais‘, ‘a‘, ‘good‘, ‘girl‘]
30.S.rsplit(sep=None, maxsplit=-1) -> list of strings
S.rsplit()与split()类似,区别是split()是从左往由开始分割,rsplit是右往左开始分割
>>> str = ‘ maliyais a good girl ‘
>>> str.split()
[‘maliyais‘, ‘a‘, ‘good‘, ‘girl‘]
>>> str.rsplit()
31. S.splitlines([keepends]) -> list of strings
S.splitlines()与s.split(‘\n‘)的区别在于\n在字符串的头部或尾部时,
s.split(‘\n‘)返回的列表中多一个空字符串
>> ‘who\nare\nyou‘.splitlines()
[‘who‘, ‘are‘, ‘you‘]
>>> ‘who\nare\nyou‘.splitlines(True)
[‘who\n‘, ‘are\n‘, ‘you‘]
>>> ‘\nwho\nare\nyou\n‘.splitlines()
[‘‘, ‘who‘, ‘are‘, ‘you‘]
>>> ‘\nwho\nare\nyou\n‘.splite()
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: ‘str‘ object has no attribute ‘splite‘
>>> ‘\nwho\nare\nyou\n‘.split()
[‘who‘, ‘are‘, ‘you‘]
>>> ‘who\nare\nyou\n‘.split()
[‘who‘, ‘are‘, ‘you‘]
>>> ‘who are you\n‘.split()
[‘who‘, ‘are‘, ‘you‘]
>>> ‘who\nare\nyou\n‘.split(‘\n‘)
[‘who‘, ‘are‘, ‘you‘, ‘‘]
>>> ‘\nwho\nare\nyou\n‘.split(‘\n‘)
[‘‘, ‘who‘, ‘are‘, ‘you‘, ‘‘]
>>> ‘\nwho\nare\nyou\n‘.splitlines()
[‘‘, ‘who‘, ‘are‘, ‘you‘]
>>> ‘who\nare\nyou\n‘.splitlines()
[‘who‘, ‘are‘, ‘you‘]
>>> ‘‘.splitlines()
[]
>>> ‘‘.split(‘\n‘)
[‘‘]
Representation | Description |
---|---|
\n |
Line Feed 换行 |
\r |
Carriage Return 回车 |
\r\n |
Carriage Return + Line Feed 回车+换行 |
\v or \x0b |
Line Tabulation |
\f or \x0c |
Form Feed 换页 |
\x1c |
File Separator 文件分隔符 |
\x1d |
Group Separator 组分隔符 |
\x1e |
Record Separator 记录分隔符号 |
\x85 |
Next Line (C1 Control Code) |
\u2028 |
Line Separator 行分隔符 |
\u2029 |
Paragraph Separator 段落分隔符号 |
32. S.startswith(prefix[, start[, end]]) -> bool
用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end指定值,则在指定范围内检查。
>>> str = ‘ai ou, bu cuo e‘
>>> str.startswith(‘ai‘)
True
>>> str.startswith(‘ii‘)
False
>>> str.startswith(‘ou‘,3,5)
True
>>> str.startswith(‘ou‘,4,5)
False
33.S.swapcase() -> str
>>> ‘BBaa‘.swapcase()
‘bbAA‘
>>> ‘BBaa^^‘.swapcase()
‘bbAA^^‘
>>> ‘BBaa^^12‘.swapcase()
‘bbAA^^12‘
32. S.title()
32. S.title() -> str
返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写。
>>> ‘what ARE you doing‘.title()
‘What Are You Doing‘
>>> ‘what ~ARE you doing‘.title()
‘What ~Are You Doing‘
34. S.zfill(width) -> str
用 ‘0‘ 填充字符串,并返回指定宽度的字符串。
正常一般是从字符串的左边开始填充,如指定长度小于字符串长度则返回原字符串
>>> ‘good‘.zfill(10)
‘000000good‘
>>> ‘good‘.zfill(3)
‘good‘
35. S.translate(table) -> str
根据 maketrans() 方法给出的字符映射转换表转换字符串中的字符。
>>> intab = ‘abcd‘
>>> outtab = ‘1234‘
>>> trantab = str.maketrans(intab,outtab) #创建映射转换表
>>> ‘afdcs‘.translate(trantab)
‘1f43s‘
标签:下标 enc color dex res 标题 com represent util
原文地址:https://www.cnblogs.com/liangyf/p/10302327.html