zoukankan      html  css  js  c++  java
  • python3.x 基础一:str字符串方法

    *字符串不能更改值

    数据类型字符串str

    • |  capitalize(...)   返回字符串中第一个字母大写
       |      S.capitalize() -> str
       |      
       |      Return a capitalized version of S, i.e. make the first character
       |      have upper case and the rest lower case.
      >>> str1='i am chinese'
      >>> str1.capitalize()
      'I am chinese'
    •  |  casefold(...)  将大写转换成小写
       |      S.casefold() -> str
       |      
       |      Return a version of S suitable for caseless comparisons.
      >>> str2='I AM A CHINESE'
      >>> str2.casefold()
      'i am a chinese'
    •  |  center(...)  第一个参数是总字符长度,第二个是补全字符,原字符串居中
       |      S.center(width[, fillchar]) -> str
       |      
       |      Return S centered in a string of length width. Padding is
       |      done using the specified fill character (default is a space)
      >>> str1.center(50,'#')
      '###################i am chinese###################'
    •  |  count(...)  计算某个字符/元素出现次数
       |      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.
      >>> str1.count('e')
      2
      >>> str1.count('i')
      2
      >>> str1.count('a')
      1
    •  |  endswith(...)  是否以某个后缀字符串结束
       |      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.
      >>> str2.endswith('SE')
      True
      >>> str2.endswith('S')
      False
      >>> str = "this is string example....wow!!!";
      >>> suffix = "is"
      >>> print (str.endswith(suffix, 2, 6))
      False
      >>> print(str[2:7])
      is is
      #字符串含有指定后缀,为什么不是返回真?
      #理解错误,起始位置是左闭右开
      >>> print (str.endswith(suffix, 2,7))
      True
    •   |  expandtabs(...) 扩展tab字符n个
       |      S.expandtabs(tabsize=8) -> str
       |      
       |      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.
      >>> str4='I am 	 Chinese'
      >>> str4.expandtabs(10)
      'I am       Chinese'
    •  |  find(...)  返回查找到的第一个字符位置,否则返回-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.
      >>> str2
      'I AM A CHINESE'
      >>> str2.find('M')
      3
      >>> str2.find('A')
      2
      >>> str2.find('B')
      -1
    •  |  index(...)  同上,查找不到则报错
       |      S.index(sub[, start[, end]]) -> int
       |      
       |      Like S.find() but raise ValueError when the substring is not found.

      >>> str2
      'I AM A CHINESE'
      >>> str2.index('A')
      2
      >>> str2.index('B')
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      ValueError: substring not found
    •  |  isalnum(...)  判断字符串是否由字母数字组成
       |      S.isalnum() -> bool
       |      
       |      Return True if all characters in S are alphanumeric
       |      and there is at least one character in S, False otherwise.
      >>> str5='I am 1 chinese'
      >>> str5.isalnum()
      False
      >>> str6='iAm1Chinese'
      >>> str6.isalnum()
      True
    •  |  isalpha(...)  判断字符串是否全部是字符组成
       |      S.isalpha() -> bool
       |      
       |      Return True if all characters in S are alphabetic
       |      and there is at least one character in S, False otherwise.
      >>> str7='iamChinese'
      >>> str7.isalpha()
      True
      >>> str3.isalpha()
      False
    •  |  isdecimal(...) 如果字符串是否只包含十进制字符返回True,否则返回False
       |      S.isdecimal() -> bool
       |      
       |      Return True if there are only decimal characters in S,
       |      False otherwise.
      >>> str8=u'asdfasdf1000'
      >>> str8.isdecimal()
      False
      >>> stru=u'1000'
      >>> stru.isdecimal()
      True
    •  |  isdigit(...)  判断字符串中是否全部是数字
       |      S.isdigit() -> bool
       |      
       |      Return True if all characters in S are digits
       |      and there is at least one character in S, False otherwise.
      >>> str9,str10='abc123','123'
      >>> str9.isdigit()
      False
      >>> str10.isdigit()
      True
    •  |  isidentifier(...) 判断变量明明是否合法
       |      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".
    •  |  islower(...)  判断字符串中所有字母是否小写
       |      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.
      >>> str11,str12,str13='abcd','AbCd','Ab123'
      >>> str11.islower()
      True
      >>> str12.islower()
      False
      >>> str13.islower()
      False
      >>> str14='aa123'
      >>> str14.islower()
      True
    •  |  isnumeric(...) 判断字符串中所有字符是否是数字
       |      S.isnumeric() -> bool
       |      
       |      Return True if there are only numeric characters in S,
       |      False otherwise.
      >>> str10,str14
      ('123', 'aa123')
      >>> str10.isnumeric()
      True
      >>> str14.isnumeric()
      False
    •  |  isspace(...) 判断字符是否是空格
       |      S.isspace() -> bool
       |      
       |      Return True if all characters in S are whitespace
       |      and there is at least one character in S, False otherwise.
      >>> str1,str14,str15
      ('i am chinese', 'aa123', ' ')
      >>> str1.isspace(),str14.isspace(),str15.isspace()
      (False, False, True)

    •  |  istitle(...) 判断字符串是否是标题,标题单词首字母均大写
       |      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.
      >>> str16,str17='I Am Chinese','I am chinese'
      >>> str16.istitle(),str17.istitle()
      (True, False)
    •  |  isupper(...)  判断字符串中字母是否全部是大写
       |      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.
      >>> str18,str19='ABC123','Abc123'
      >>> str18.isupper(),str19.isupper()
      (True, False)
    •  |  join(...) 将其他可迭代对象转换成字符串,S表示字符串分隔符
       |      S.join(iterable) -> str
       |      
       |      Return a string which is the concatenation of the strings in the
       |      iterable.  The separator between elements is S.
      >>> list1=['a','b','c','d']
      >>> ','.join(list1)
      'a,b,c,d'
      >>> ''.join(list1)
      'abcd'
      >>> ' '.join(list1)
      'a b c d'
    •  |  ljust(...)  共width个位置,字符串靠左,右边以字符填充
       |      S.ljust(width[, fillchar]) -> str
       |      
       |      Return S left-justified in a Unicode string of length width. Padding is
       |      done using the specified fill character (default is a space).
      >>> str10
      '123'
      >>> str10.ljust(10,'#')
      '123#######'
    •  |  lower(...)  将字符串中所有大写字母转换成小写
       |      S.lower() -> str
       |      
       |      Return a copy of the string S converted to lowercase.
      View Code
    •  |  lstrip(...)  将左边空格剪掉,可以指定其他被剪掉空格,
       |      S.lstrip([chars]) -> str
       |      
       |      Return a copy of the string S with leading whitespace removed.
       |      If chars is given and not None, remove characters in chars instead.
      >>> str20='  123abc  ABC  '
      >>> str20.lstrip()
      '123abc  ABC  '

      strip()将左右两边空格剪掉,rstrip()将右边空格剪掉

    •  |  partition(...) 以某个字符切片字符串,返回三元组,中间是分隔符,找不到则返回源字符串和2个空格
       |      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.
      >>> str21='http://www.baidu.com'
      >>> str21.partition('://'... )
      ('http', '://', 'www.baidu.com')
      >>> str21.partition('abc')
      ('http://www.baidu.com', '', '')
    •  |  replace(...)  字符替换,默认全部替换,如指定计数则替换指定计数个数字符
       |      S.replace(old, new[, count]) -> str
       |      
       |      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.
      >>> str21
      'http://www.baidu.com'
      >>> str21.replace('baidu','jd')
      'http://www.jd.com'
      >>> str21.replace('w','z')
      'http://zzz.baidu.com'
      >>> str21.replace('w','z',1)
      'http://zww.baidu.com'
    •  |  rfind(...) 从左往右查找,并返回最右边的字符索引位置
       |      S.rfind(sub[, start[, end]]) -> int
       |      
       |      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.
      >>> str21
      'http://www.baidu.com'
      >>> str21.rfind('b')
      11
      >>> str21.rfind('w')
      9
    •  |  rindex(...)
       |      S.rindex(sub[, start[, end]]) -> int
       |      
       |      Like S.rfind() but raise ValueError when the substring is not found.

    •  |  rjust(...) 字符靠右,左边补全字符,默认空格
       |      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).
       |  rjust(...)
       |      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).
      >>> str10.rjust(10,'x')
      'xxxxxxx123'
    •  |  rpartition(...) 无法找到切片字符串,则源字符串索引是2,前面是空格
       |      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.
      >>> str21.rpartition('://')
      ('http', '://', 'www.baidu.com')
      >>> str21.rpartition('abc')
      ('', '', 'http://www.baidu.com')
    •   |  split(...) 默认分隔符空格 ,可以指定分割次数
       |      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.
      >>> str21.split('w')
      ['http://', '', '', '.baidu.com']
      >>> str21.split('w',1)
      ['http://', 'ww.baidu.com']
      >>> str21.split('w',2)
      ['http://', '', 'w.baidu.com']
      >>> str21.split('w',3)
      ['http://', '', '', '.baidu.com']
    • |  rsplit(...) 右边第一个分割符开始切分,默认全切分,可以指定分割次数
       |      S.rsplit(sep=None, maxsplit=-1) -> list of strings
       |      
       |      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.
      >>> str21.rsplit('w')
      ['http://', '', '', '.baidu.com']
      >>> str21.rsplit('w',1)
      ['http://ww', '.baidu.com']
      >>> str21.rsplit('w',2)
      ['http://w', '', '.baidu.com']
      >>> str21.rsplit('w',3)
      ['http://', '', '', '.baidu.com']
    •  |  splitlines(...) 行分割
       |      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.
      >>> str22='www
      baidu
      com'
      >>> str22.splitlines()
      ['www', 'baidu', 'com']
    •  |  startswith(...)  同endswith()
       |      S.startswith(prefix[, start[, end]]) -> bool
       |      
       |      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.
    •  |  swapcase(...)   字符串中大小写转换,大写变小写,小写变大写--- upper(),lower()
       |      S.swapcase() -> str
       |      
       |      Return a copy of S with uppercase characters converted to lowercase
       |      and vice versa.
      >>> str23='asdf123'
      >>> str23.swapcase()
      'ASDF123'
      >>> str24='ABcd222'
      >>> str24.swapcase()
      'abCD222'
    •  |  title(...)  返回一个titile标题
       |      S.title() -> str
       |      
       |      Return a titlecased version of S, i.e. words start with title case
       |      characters, all remaining cased characters have lower case.
      >>> str25='a good persion asdf 213'
      >>> str25.title()
      'A Good Persion Asdf 213'
    •  |  zfill(...)  总长width个字符,前面补0
       |      S.zfill(width) -> str
       |      
       |      Pad a numeric string S with zeros on the left, to fill a field
       |      of the specified width. The string S is never truncated.
      >>> str25.zfill(30)
      '0000000a good persion asdf 213'
  • 相关阅读:
    Objective Evaluation Index of image
    MATLAB奔溃仅左上角显示关闭界面X
    论文修改指南之发表
    科研第一步:文献查找和下载
    实习项目1-串口IP升级调试
    FPGA驱动LCD显示红绿蓝彩条
    tw9912配置使用
    BT.656视频信号解码
    色彩空间转换 rgb转ycbcr422/ycbcr422转rgb
    xilinx VDMA IP核使用
  • 原文地址:https://www.cnblogs.com/jenvid/p/7808498.html
Copyright © 2011-2022 走看看