zoukankan      html  css  js  c++  java
  • str类

        def capitalize(self):  
            """ 首字母变大写,其余字母一律变小写 """
            """
            S.capitalize() -> string
            
            Return a copy of the string S with only its first character
            capitalized.
            """
            return ""
      for example
          s = 'abcd'
          s.capitalize()----->'Abcd'
       s = 'abCsd'
       s.lower()           #全部变小写字母 -> abcsd
       s.casefold()        #全部变小写字母,相较于lower更强大,能转换多种语言的大小写 -> abcsd
       s.upper()           #全部变大写字母 -> ABCSD
       s.capitalize()      #一个字符串中只有第一个字母大写,其余一律小写 -> Abcsd
        def center(self, width, fillchar=None):  
            """ 内容居中,width:总长度;fillchar:空白处填充内容,并且只能填一个字符,默认无 """
            """
            S.center(width[, fillchar]) -> string
            
            Return S centered in a string of length width. Padding is
            done using the specified fill character (default is a space)
            """
            return ""
            for example
            s = 'adaf'
            s.center(20,'*') -> ********adaf********
            总共20位的空间,用‘*’取代空白处
        扩展: s.ljust(20,'*') -> adaf****************
             s.rjust(20,'*') -> ****************adaf
     def count(self, sub, start=None, end=None):  
            """ 子序列个数 """
            """
            S.count(sub[, start[, end]]) -> int
            
            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.
            """
            return 0
    for example
    a = 'aaadd'
    a.count('a')   -> 3
    a.count('d',3) -> 2
    a.count('d',3,4) -> 1      由上式可知,取值区间左闭右开
    def expandtabs(self, tabsize=None):  
            """ 将tab转换成空格,默认一个tab转换成8个空格 """
            """
            S.expandtabs([tabsize]) -> string
            
            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.
            """
            return ""
    for example
    a = 'aaa	dddd	dd'
    a.expandtabs() -> aaa     dddd    dd
    可见以8位为一个单位,不足的用空格替换,这对于表格对齐有用
    def endswith(self, suffix, start=None, end=None):  
            """ 是否以 xxx 结束 """
            """
            S.endswith(suffix[, start[, end]]) -> bool
            
            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.
            """
            return False
    for example
    a = '012345'
    a.endswith('5') -> True
    a.endswith('5',3) -> True
    a.endswith('45') -> True
    a.endswith('345') -> True
    a.endswith('2345') -> False

    同理还有startswith()
    def find(self, sub, start=None, end=None):  
            """ 寻找子序列位置,如果没找到,返回 -1 """
            """
            S.find(sub [,start [,end]]) -> int
            
            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.
            """
            return 0
    for example
    a = 'abcd'
    a.find('c') -> 2
    a.find('c',1,2) -> -1
    找值区间遵循左闭右开
    def format(*args, **kwargs): # known special case of str.format
            """ 字符串格式化,动态参数,将函数式编程时细说 """
            """
            S.format(*args, **kwargs) -> string
            
            Return a formatted version of S, using substitutions from args and kwargs.
            The substitutions are identified by braces ('{' and '}').
            """
            pass
    for example
    a = 'i am {name}'
    print(a)  -> i am name
    print(a.format(name = 'pig')) -> i am pig    将字符串的占位符替换为输入的值
    def isalnum(self):  
            """ 是否是字母和数字 """
            """
            S.isalnum() -> bool
            
            Return True if all characters in S are alphanumeric
            and there is at least one character in S, False otherwise.
            """
            return False
    
        def isalpha(self):  
            """ 是否是字母 """
            """
            S.isalpha() -> bool
            
            Return True if all characters in S are alphabetic
            and there is at least one character in S, False otherwise.
            """
            return False
    
        def isdigit(self):  
            """ 是否是数字 """
            """
            S.isdigit() -> bool
            
            Return True if all characters in S are digits
            and there is at least one character in S, False otherwise.
            """
            return False
    
        def islower(self):  
            """ 是否小写 """
            """
            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.
            """
            return False
    
        def isspace(self):  
            """
            S.isspace() -> bool
            
            Return True if all characters in S are whitespace
            and there is at least one character in S, False otherwise.
            """
            return False
    
        def istitle(self):  
            """
            S.istitle() -> bool
            
            Return True if S is a titlecased string and there is at least one
            character in S, i.e. uppercase characters may only follow uncased
            characters and lowercase characters only cased ones. Return False
            otherwise.
            """
            return False
    def join(self, iterable):  
            """ 连接 """
            """
            S.join(iterable) -> string
            
            Return a string which is the concatenation of the strings in the
            iterable.  The separator between elements is S.
            """
            return ""
    for example
    v = '_'join('adc') -> a_d_c
    def lstrip(self, chars=None):  
            """ 移除左侧空白 """
            """
            S.lstrip([chars]) -> string or unicode
            
            Return a copy of the string S with leading whitespace removed.
            If chars is given and not None, remove characters in chars instead.
            If chars is unicode, S will be converted to unicode before stripping
            """
            return ""
    同类的有    rstrip()    strip()  传入字符可以移除指定字符
    def translate(self, table, deletechars=None):  
            """
            转换,需要先做一个对应表,最后一个表示删除字符集合
            intab = "aeiou"
            outtab = "12345"
            trantab = maketrans(intab, outtab)
            str = "this is string example....wow!!!"
            print str.translate(trantab, 'xm')   -> m1k2tr1ns
            """
    
            """
            S.translate(table [,deletechars]) -> string
            
            Return a copy of the string S, where all characters occurring
            in the optional argument deletechars are removed, and the
            remaining characters have been mapped through the given
            translation table, which must be a string of length 256 or None.
            If the table argument is None, no translation is applied and
            the operation simply removes the characters in deletechars.
            """
            return ""
     def split(self, sep=None, maxsplit=None):  
            """ 分割, maxsplit最多分割几次 """
            """
            S.split([sep [,maxsplit]]) -> list of strings
            
            Return a list of the words in the string 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.
            """
            return []
    def partition(self, sep):  
            """ 分割,前,中,后三部分 """
            """
            S.partition(sep) -> (head, sep, tail)
            
            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.
            """
            pass
     def swapcase(self):  
            """ 大写变小写,小写变大写 """
            """
            S.swapcase() -> string
            
            Return a copy of the string S with uppercase characters
            converted to lowercase and vice versa.
            """
            return ""
    def replace(self, old, new, count=None):  
            """ 替换 """
            """
            S.replace(old, new[, count]) -> string
            
            Return a copy of string S with all occurrences of substring
            old replaced by new.  If the optional argument count is
            given, only the first count occurrences are replaced.
            """
            return ""

    注意:字符串的修改,拼接,不会再原字符串中操作,而是在新的内存里生成

  • 相关阅读:
    404 页面不存在
    Elasticsearch 目录总结
    Excel 数据导入至Sqlserver 数据库中 ltrim() 、rtrim() 、replace() 函数 依次空格无效问题
    自动化测试工具-Selenium IDE 教程一
    搜索引擎背后的经典数据结构和算法
    如何收缩SQLServer 数据库日志文件大小?
    如何处理自我感动【转载】
    Microsoft.Jet.OLEDB.4.0 和 Microsoft.ACE.OLEDB.12.0 的区别
    windows 2012 IIS 部署 .net core HTTP Error 502.5
    Windows Server2012 KB2919355 补丁无法安装
  • 原文地址:https://www.cnblogs.com/ighuahua/p/9477862.html
Copyright © 2011-2022 走看看