python 内置数据操作(一)--- 字符串str

S.capitalize() -> str
        
        Return a capitalized version of S, i.e. make the first character
        have upper case and the rest lower case.
返回一个大写的字符串,将字符串的首字母转换为大写,其余的转换为小写。
S.casefold() -> str
        
        Return a version of S suitable for caseless comparisons.
返回一个适用于不分大小写对比的字符串,其实就是把大写字母都变成小写字母
S.center(width[, fillchar]) -> str

        width -- 字符串的总宽度。
        fillchar -- 填充字符。   
         
        Return S centered in a string of length width. Padding is
        done using the specified fill character (default is a space)
返回一个以原始字符串为中心、以填充字符(fillchar)为两边的字符串,返回字符的总长度为width,如果原始字符串长度大于width,则返回原字符串。
S.count(sub[, start[, end]]) -> int
        
        sub -- 子字符串
        start,end -- 原始字符串切片的起始和结束位置(索引)

        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.
返回原始字符串在切片索引范围内包含子字符串的个数,当start(end)为None时,即为从头开始(到尾结束)。
S.encode(encoding='utf-8', errors='strict') -> bytes
        
        encoding -- 编码格式
        errors -- 错误处理方案 

        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.  
以指定的编码将字符串转换为bytes,其中encoding默认为utf
-8,errors指发生错误时的处理方案,默认strict会出错会抛出UnicodeEncodeError异常,其它值还有:ingnore、replace和xmlcharrefreplace,这些error处理方法可以处理UnicodeEncodeError异常。
S.endswith(suffix[, start[, end]]) -> bool

        suffix -- 后缀,字符串或者以字符串为元素的元组,不可以是列表
        start, end -- 原始字符串的切片起始位置

        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.

        如果原始字符串以suffix字符串或其某个元素结尾,则返回True,否则返回False。当start、end不为None时,则判断的是原始字符串以start和end切片后的情况。suffix可以是一个以字符串为元素的元组。
S.expandtabs(tabsize=8) -> str

        tabsize -- 将\t转为空格的个数

        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.

        原始字符串不变,返回一个copy的、\t被空格替换了的字符串,默认一个\t转换成8个空格,可以通过tabsize参数进行修改。
S.find(sub[, start[, end]]) -> int

        sub -- 要做查找的子字符串
        start,end -- 同上

        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.

        返回子字符串所在的最小的索引,可以通过start和end进行切片查找,如果原始字符串不包含sub,则返回-1
S.format(*args, **kwargs) -> str
        
        Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}').

        在原始字符串中埋下格式为{}的占位字符:
        当args时,{}中通过放置0,1,2...等索引进行格式化,如{0}、{1}分别对应args中的索引为0和1的数据;
        当kwargs时,则通过字典做映射关系,在{}中放置kwargs中的key,将该key对应的value格式化到原始字符串中,如{‘key1’}、{‘key2’}对应kwargs={‘key1’:‘value1’,‘key2’:‘value2’}中对应的value值
     返回格式化好的字符串
S.format_map(mapping) -> str

        mapping -- 映射关系,即字典格式的映射
        
        Return a formatted version of S, using substitutions from mapping.
        The substitutions are identified by braces ('{' and '}').

        返回格式化好的字符串,同format中的kwargs格式化。
 S.index(sub[, start[, end]]) -> int
    
        sub -- 同find
        start、end -- 同find
        
        Like S.find() but raise ValueError when the substring is not found.

        和find功能一样,但是在原始字符串不包含sub时会引发ValueError 的异常。
S.isalnum() -> bool
        
        Return True if all characters in S are alphanumeric
        and there is at least one character in S, False otherwise.

        判断字符串是否全部为字母数字,返回判断结果
S.isalpha() -> bool
        
        Return True if all characters in S are alphabetic
        and there is at least one character in S, False otherwise.

        判断字符串是否只由字母组成,并返回判断结果。
S.isdecimal() -> bool
        
        Return True if there are only decimal characters in S,
        False otherwise.

        判断字符串是否只包含十进制字符,返回判断结果。用于对unicode对象的判断,如u‘123’等
S.isdigit() -> bool
        
        Return True if all characters in S are digits
        and there is at least one character in S, False otherwise.

        判断字符串是否只包含数字,返回判断结果
S.isnumeric() -> bool
        
        Return True if there are only numeric characters in S,
        False otherwise.

        判断字符串是否只包含数字字符,返回判断结果
关于isdecimal、isdigit、isnumeric三个方法的区别如下:
isdigit()
True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
False: 汉字数字
Error: 无

isdecimal()
True: Unicode数字,,全角数字(双字节)
False: 罗马数字,汉字数字
Error: byte数字(单字节)

isnumeric()
True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
False: 无
Error: byte数字(单字节)

a = '④⑺⒎Ⅶ❽㈦❺'
print(a.isnumeric())
结果仍然为True,所以numeric对字符中的数字识别最强,digit可以可以bytes类型,decimal只识别十进制。
S.isidentifier() -> bool
        
        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".

        判断字符串是否为python标识符,返回判断结果。
        标识符即类似:def、class等已经被占用的关键字,作用和如下内置函数一样:
        import keyword
        keyword.iskeyword('def')
S.islower() -> bool
        
        Return True if all cased characters in S are lowercase and there is at least one cased character in S, False otherwise.

        判断字符串是否都为小写字母。该字符串中至少有一个字母。
S.isprintable() -> bool
        
        Return True if all characters in S are considered printable in repr() or S is empty, False otherwise.

        判断字符串是否为可打印出来的,空字符也会返回True。
S.isspace() -> bool
        
        Return True if all characters in S are whitespace and there is at least one character in S, False otherwise.
    
        判断不为空的字符串是否都为空白字符。
S.istitle() -> bool
        
        Return True if S is a titlecased string and there is at least one character in S, i.e. upper- and titlecase characters may only
follow uncased characters and lowercase characters only cased ones.Return False otherwise.
    
     判断非空字符是否为标题格式,即每个单词首字母大写,其它字母小写的格式。每个被空格、tab、换行等whitespace隔开的,都算一个单词,必须每个单词都符合格式才会返回True。
S.isupper() -> bool
        
        Return True if all cased characters in S are uppercase and there is at least one cased character in S, False otherwise.

        非空字符串全为大写的时候,返回True,否则为False。
S.join(iterable) -> str

        iterable -- 要链接的字符串可迭代参数 
        
        Return a string which is the concatenation of the strings in the iterable.  The separator between elements is S.   

         将可迭代参数里的元素通过原始字符串连接起来,并返回该拼接好的字符串,原始字符串不变。如果参数为字典,则拼接的是字典的key,并且拼接顺序是随机的。
S.ljust(width[, fillchar]) -> str

        width -- 填充后的字符总长度
        fillchar -- 填充的字符,one character
        
        Return S left-justified in a Unicode string of length width. Padding is done using the specified fill character (default is a space).

        给字符串尾部加上填充的字符,并返回。其中fillchar只能是一个字符,默认为一个空格。
S.lower() -> str
        
        Return a copy of the string S converted to lowercase.

        把字符串里的字符都转换成小写字母后返回
S.lstrip([chars]) -> str

        chars -- 需要清除掉的字符串
        
        Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.

        将字符串拷贝后,如果copy字符串是以chars字符开头的,则清除并返回copy字符串,否则直接返回原字符串。
def maketrans(self, *args, **kwargs)

Return a translation table usable for str.translate().
        
       If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None.Character keys will be then converted to ordinals. 
    If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to th character at the same position in y.
    If there is a third argument, it must be a string, whose characters will be mapped to None in the result. 如果只有一个参数,那么它必须是一个映射Unicode序列(整数)或字符到Unicode序列、字符串或None的字典。字符键将被转换为序列。 如果有两个参数,它们必须是相等长度的字符串,在生成的字典中,x中的每个字符将被映射到y中的同一位置上的字符。 如果有第三个参数,则必须是字符串,其字符将在结果中映射到None。 此函数是仅用于为S.translate方法提供一个映射表,本身没有什么直接作用。
S.partition(sep) -> (head, sep, tail)

        sep -- 分隔符参数,为字符串格式
        
        Search for the separator sep in S, and return the part before it,the separator itself, and the part after it.  If the separator is not found, return S and two empty strings.

        在字符中查找sep分隔符,如果字符串包含sep子字符串,则返回一个包含分隔前一段,分隔符sep以及分隔符后一段的元组,如果不包含,则返回一个包含原始字符和两个空字符串的元组。
S.replace(old, new[, count]) -> str

        old -- 要被替换的字符
        new -- 要替换成的字符
        count -- 替换次数, 默认为None,即替换所有
        
        Return a copy of S with all occurrences of substring old replaced by new.  If the optional argument count is given, only the first count occurrences are replaced.

       将一个原始字符的拷贝进行子字符串替换,将old参数字符更换为new参数字符,count为None时替换所有,否则count为几个则替换几次,并返回该copy字符串,如果原始字符串不包含old字符,则返回原始字符串。  
S.rfind(sub[, start[, end]]) -> int

        sub、start、end -- 同find方法
        
        Return the highest 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.    
    
        在目标字符串或其切片字符串中,从右往左找包含sub子字符串的索引,并返回该索引值(正向索引),如果不包含该sub子字符串,则返回-1。
S.rindex(sub[, start[, end]]) -> int

    
     sub、start、end -- 同find方法
     Like S.rfind() but raise ValueError when the substring is not found.
    
     作用和rfind一样,不过在原字符串不包含sub字符串是会抛出ValueError的异常。
S.rjust(width[, fillchar]) -> str
        
        Return S right-justified in a string of length width. Padding is done using the specified fill character (default is a space).

     类似ljust,填充方向相反。
S.rpartition(sep) -> (head, sep, tail)
        
        Search for the separator sep in S, starting at the end of S, and return the part before it, the separator itself, and the part after it.  If the separator is not found, return two empty strings and S.

     类似partition,r代表right,区别是从后往前找,找到第一个然后分隔一下。
S.rsplit(sep=None, maxsplit=-1) -> list of strings
        
     sep -- 用来分隔的字符串
     maxsplit --
     Return a list of the words
in S, using sep as the delimiter string, starting at the end of the string and working to the front.
     If maxsplit
is given, at most maxsplit splits are done. If sep is not specified, any whitespace string is a separator.

     从右往左对字符串进行去sep字符串操作,并把去掉sep后分隔开的几个部分作为元组元素返回。sep=None时则以空格为sep,maxsplit为做大去除次数,默认为全部去除。特别注意,如果原始字符串中有相邻的sep字符串,则会出现‘’在返回元组的元素里。
S.rstrip([chars]) -> str
        
     chars -- 要清除的字符
Return a copy of the string S with trailing whitespace removed.If chars
is given and not None, remove characters in chars instead.
    
     类似lstrip,区别是清除尾部的chars。
S.split(sep=None, maxsplit=-1) -> list of strings
        
        Return a list of the words in S, using sep as the delimiter string.If maxsplit is given, at most maxsplit splits are done. 
     If sep
is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

     类似rsplit,区别是从左往右执行。注意的是,当全部替换时,这两种方法的返回值是一样的
S.splitlines([keepends]) -> list of strings
        
        Return a list of the lines in S, breaking at line boundaries.Line breaks are not included in the resulting list unless keepends is given and true.
    
     按/n换行来分隔字符串,将分隔的字符串组成列表返回。字符串不包含\n时,则返回一个以原字符串为元素的列表
S.startswith(prefix[, start[, end]]) -> bool
    
     prefix -- 用来做判断的参数,可以是字符串或者由字符串组成的元组,不可以是列表
     start、end -- 同endswith Return True
if S starts with the specified prefix, False otherwise.With optional start, test S beginning at that position.With optional end, stop comparing S at that position.
     prefix can also be a tuple of strings to
try.

     判断字符或字符切片是否以prefix,或prefix的元素开头,返回判断结果。
S.strip([chars]) -> str
        
        chars -- 要去除的字符

        Return a copy of the string S with leading and trailing
        whitespace removed.
        If chars is given and not None, remove characters in chars instead.

         copy原字符串,并对copy字符串的头和尾进行去空白符操作并返回。如果参数chars不为空,则去除chars字符后返回。
S.swapcase() -> str
        
        Return a copy of S with uppercase characters converted to lowercase and vice versa.

        copy一份字符串,并对copy字符串中的字符小写变大写,大写变小写,返回结果。
S.title() -> str
        
        Return a titlecased version of S, i.e. words start with title case  characters, all remaining cased characters have lower case.

        将字符串变成标题格式,即将每个单词的首字母大写,其余字母小写,并返回结果。单词由空白符隔开。
S.translate(table) -> str

        table -- maptable,一般是上边描述的maketrans对象
        
        Return a copy of the string S in which each character has been mapped through the given translation table. 
     The table must implement lookup/indexing via __getitem__, for instance a dictionary or list,mapping Unicode ordinals to Unicode ordinals, strings, or None.
     If this operation raises LookupError, the character is left untouched.Characters mapped to None are deleted.
S.upper() -> str
        
        Return a copy of S converted to uppercase.

        将字符串copy的字符全部变成大写后返回。
S.zfill(width) -> str

        width -- 填充后字符串的长度
        
        Pad a numeric string S with zeros on the left, to fill a field of the specified width. The string S is never truncated.

        用0在原字符串(numeric字符串)左侧进行填充,width为填充后字符串长度,并返回。

转载于:https://www.cnblogs.com/by3333/p/7700950.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值