str内置函数梳理(一)

函数名:capitalize
S.capitalize() -> str
#将首字母大写,其他大写的字母,会转化小写
>>> s='abcDRF'
>>> s.capitalize()
'Abcdrf'
Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.


函数名:casefold
S.casefold() -> str
#将字符串里大写转化成小写
>>> s='abcDRF'
>>> s.casefold()
'abcdrf'
>>> s='abc'
>>> s.casefold()
'abc'
Return a version of S suitable for caseless comparisons.


函数名:center
S.center(width[, fillchar]) -> str
#传入的宽度(形参可以传入填充的符号),字符串居中填充,默认是空格
>>> s='abc'
>>> s.center(10,'*')
'***abc****'
Return S centered in a string of length width. Padding is
done using the specified fill character (default is a space)


函数名:count
S.count(sub[, start[, end]]) -> int
#计算sub在str中出现次数可以指定区域
>>> s='abcaasdsdsadsfaaDRF'
>>> s.count('a')
6
>>> s.count('a',2,6)
2
Return the number of non-overlapping occurrences of substring sub in
string S[start:end].  Optional arguments start and end are
interpreted as in slice notation.


函数名:encode
S.encode(encoding='utf-8', errors='strict') -> bytes
#编码,默认是utf-8
Encode S using the codec registered for encoding. Default encoding
is 'utf-8'. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that can handle UnicodeEncodeErrors.


函数名:endswith
S.endswith(suffix[, start[, end]]) -> bool
#结束以suffix结尾。返回bool值
>>> s='abcefg'
>>> s.endswith('g')
True
>>> s.endswith('g',0,4)
False
Return True if S ends with the specified suffix, False otherwise.
With optional start, test S beginning at that position.
With optional end, stop comparing S at that position.
suffix can also be a tuple of strings to try.


函数名:expandtabs
S.expandtabs(tabsize=8) -> str
#字符串中有空格,将空格的占位改动,默认是8
>>> s='a\tb\tch'
>>> s.expandtabs(4)
'a   b   ch'
>>> s.expandtabs(8)
'a       b       ch'
Return a copy of S where all tab characters are expanded using spaces.
If tabsize is not given, a tab size of 8 characters is assumed.


函数名:find
S.find(sub[, start[, end]]) -> int
#在str中寻找sub,可以指定范围,返回找到的第一个的索引,失败时返回-1
>>> s='abcefaag'
>>> s='abaacefaag'
>>> s.find('a',0,4)
0
Return the lowest index in S where substring sub is found,
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.
Return -1 on failure.


函数名:format
S.format(*args, **kwargs) -> str
#格式化输出,序列,字典形式传入
>>> 'asds{0}ad{1}nh'.format(11,22)
'asds11ad22nh'
>>> "Hello {name}, your balance is {blc}.".format(name="Adam", blc=230.2346)
Hello Adam, your balance is 230.2346.
Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces ('{' and '}').


函数名:format_map
S.format_map(mapping) -> str
#类似fotmat
Return a formatted version of S, using substitutions from mapping.
The substitutions are identified by braces ('{' and '}').


函数名:index
S.index(sub[, start[, end]]) -> int
#str中sub的索引,指定范围,失败时会报错。对比find
>>> s='abcefaahjghgjdfgrteg'
>>> s.index('aa')
5
>>> s.index('aa',6,9)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
Return the lowest index in S where substring sub is found, 
such that sub is contained within S[start:end].  Optional
arguments start and end are interpreted as in slice notation.
Raises ValueError when the substring is not found.


函数名:isalnum
S.isalnum() -> bool
#判断字符串都是字母或者数字,返回bool
>>> s='abcefaahjghgjdfgrteg'
>>> s.isalnum()
True
>>> s='11as1'
>>> s.isalnum()
True
>>> s='11as  1'
>>> s.isalnum()
False
Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.


函数名:isalpha
S.isalpha() -> bool
#判断字符串是否都是字母(大小写都行),返回bool
>>> s= "Monica"
>>> s.isalpha()
 True
>>> s= "Monica  Geller"
>>> s.isalpha()
False
Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.


函数名:isdecimal
S.isdecimal() -> bool
#如果字符串中的所有字符都是十进制字符,则为真
>>> s= "1566"
>>> s.isdecimal()
 True
>>> s= "Monica  Geller"
>>> s.isdecimal()
False
Return True if there are only decimal characters in S,
False otherwise.


函数名:isdigit
S.isdigit() -> bool
#判断是否都是数字
>>> s='11a1'
>>> s.isdigit()
False
>>> s='111'
>>> s.isdigit()
True
Return True if all characters in S are digits
and there is at least one character in S, False otherwise.


函数名:isidentifier
S.isidentifier() -> bool
#判断是否是有效标识符(字母开头,无空格等??)
>>> s='111'
>>> s.isidentifier()
False
>>> s='a111'
>>> s.isidentifier()
True
>>> s='a 111'
>>> s.isidentifier()
False
>>> s='aaasds'
>>> s.isidentifier()
True
Return True if S is a valid identifier according
to the language definition.
Use keyword.iskeyword() to test for reserved identifiers
such as "def" and "class".

函数名:islower
S.islower() -> bool
#判断是否都是小写,返回bool
>>> s='aaasds'
>>> s.islower()
True
>>> s='aa!!!!asds'
>>> s.islower()
True
>>> s='aa!!!!asSs'
>>> s.islower()
False
Return True if all cased characters in S are lowercase and there is
at least one cased character in S, False otherwise.


函数名:isnumeric
S.isnumeric() -> bool
#判断是否都是数字(在Python中,十进制字符(如:0,1,2 ..)、数字(如:下标、上标)和具有Unicode数字值属性的字符(如:分数、罗马数字、货币数字符)都被认为是数字字符。)
>>> s = '1242323'
>>> s.isnumeric()
True
#s = '²3455'
>>> s = '\u00B23455'
>>> s.isnumeric()
True
# s = '½'
>>> s = '\u00BD'
>>> s.isnumeric()
True
>>> s = 'p1242323'
>>> s.isnumeric()
False
Return True if there are only numeric characters in S,
False otherwise.

猜你喜欢

转载自blog.csdn.net/abb1513/article/details/80265106