6.python字符串-内置方法列举

  所谓内置方法,就是凡是字符串都能用的方法,这个方法在创建字符串的类中,下面是总结:

  首先,我们要学习一个获取帮助的内置函数 help(对象) ,对象可以是一个我们创建出来的,也可以是创建对象的那个类,类也是一个对象,被称为类对象。

  当我们进入解释器的交互模式中输入以下代码时:

help(str)

  其中,str就是创建字符串的类,然后我们就会得到一长串的结果:

Help on class str in module __builtin__:

class str(basestring)
 |  str(object='') -> string
 |  
 |  Return a nice string representation of the object.
 |  If the argument is a string, the return value is the same object.
 |  
 |  Method resolution order:
 |      str
 |      basestring
 |      object
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y  #字符串拼接,看+号就知道
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x  #判断x里字符是否在y里
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y  
 |  
 |  __format__(...)
 |      S.__format__(format_spec) -> string
 |      
 |      Return a formatted version of S as described by format_spec.
 | 
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name  #获取属性
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]  #索引取值,详情参考python中的序列
 |  
 |  __getnewargs__(...)
 |  
 |  __getslice__(...)
 |      x.__getslice__(i, j) <==> x[i:j]  #切片,也是序列的一种方法
 |      
 |      Use of negative indices is not supported.
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)  
 |  
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 |  
 |  __len__(...)
 |      x.__len__() <==> len(x)
 |  
 |  __lt__(...)
 |      x.__lt__(y) <==> x<y
 |  
 |  __mod__(...)
 |      x.__mod__(y) <==> x%y
 |  
 |  __mul__(...)
 |      x.__mul__(n) <==> x*n
 |  
 |  __ne__(...)
 |      x.__ne__(y) <==> x!=y
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  __rmod__(...)
 |      x.__rmod__(y) <==> y%x
 |  
 |  __rmul__(...)
 |      x.__rmul__(n) <==> n*x
 |  
 |  __sizeof__(...)
 |      S.__sizeof__() -> size of S in memory, in bytes  #用字节表示在内存中的大小
 |  
 |  __str__(...)
 |      x.__str__() <==> str(x)
 |  
 |  capitalize(...)
 |      S.capitalize() -> string
 |      '''返回首字母大写字符串副本,对中文无效'''
 |      Return a copy of the string S with only its first character
 |      capitalized.
 |  
 |  center(...)
 |      S.center(width[, fillchar]) -> string
 |      '''返回指定宽度(width)的字符串副本,原字符串居中对齐,可指定用什么来填充多余部分(fillchar)默认为空格,关于对齐和填充可以看上篇博文的解释'''
 |      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
 |      '''计数器,返回给定字符串在原字符串出现的次数,也可指定范围'''
 |      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.
 |  
 |  decode(...)
 |      S.decode([encoding[,errors]]) -> object
 |      '''解码,将字符串解码成某种字符集'''
 |      Decodes S using the codec registered for encoding. encoding defaults
 |      to the default encoding. errors may be given to set a different error
 |      handling scheme. Default is 'strict' meaning that encoding errors raise
 |      a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
 |      as well as any other name registered with codecs.register_error that is
 |      able to handle UnicodeDecodeErrors.
 |  
 |  encode(...)
 |      S.encode([encoding[,errors]]) -> object
 |      '''编码,将字符串编码'''
 |      Encodes S using the codec registered for encoding. encoding defaults
 |      to the default encoding. 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 is able to handle UnicodeEncodeErrors.
 |  
 |  endswith(...)
 |      S.endswith(suffix[, start[, end]]) -> bool
 |      '''判断字符串在某范围内(范围用索引指定,不指定默认是整个字符串)是否以指定的字符串(suffix)结尾,返回布尔值'''
 |      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]) -> string
 |      '''将字符串里的制表符(一般用tab建输入,也可以手动使用特殊字符 \t )转换成空格,默认一个制表符转换成8个空格,也可以指定个数(tabsize),并返回一个转换后的副本'''
 |      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
 |      '''在原字符串一定范围内,寻找给定的字符串,找到了返回第一个被找到的字符的索引值,没找到就返回-1'''
 |      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) -> string
 |      '''字符串格式化(比%s更为高级,需要的话自行去了解),(*args, **kwargs)是处理函数传参的一种方式,以后继续讲'''
 |      Return a formatted version of S, using substitutions from args and kwargs.
 |      The substitutions are identified by braces ('{' and '}').
 |  
 |  index(...)
 |      S.index(sub [,start [,end]]) -> int
 |      '''和S.find()作用一样,只不过没找到时会报错'''
 |      Like S.find() but raise ValueError when the substring is 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.
 |  
 |  isalpha(...)
 |      S.isalpha() -> bool
 |      '''判断字符串是否全是由字母组成,返回布尔值'''
 |      Return True if all characters in S are alphabetic
 |      and there is at least one character in S, False otherwise.
 |  
 |  isdigit(...)
 |      S.isdigit() -> bool
 |      '''判断字符串是否全是由数字组成,返回布尔值'''
 |      Return True if all characters in S are digits
 |      and there is at least one character in S, False otherwise.
 |  
 |  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.
 |  
 |  isspace(...)
 |      S.isspace() -> bool
 |      '''判断字符串是否都是由空白字符组成(空格),当然,前提是字符串里面最少有一个空格,返回布尔值'''
 |      Return True if all characters in S are whitespace
 |      and there is at least one character in S, False otherwise.
 |  
 |  istitle(...)
 |      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.
 |  
 |  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.
 |  
 |  join(...)
 |      S.join(iterable) -> string
 |      '''字符串的拼接,详情看我前面的博文'''
 |      Return a string which is the concatenation of the strings in the
 |      iterable.  The separator between elements is S.
 |  
 |  ljust(...)
 |      S.ljust(width[, fillchar]) -> string
 |      '''左对齐,关于对齐和填充可以看我上篇博文'''
 |      Return S left-justified in a string of length width. Padding is
 |      done using the specified fill character (default is a space).
 |  
 |  lower(...)
 |      S.lower() -> string
 |      '''返回一个全是小写的字符串副本'''
 |      Return a copy of the string S converted to lowercase.
 |  
 |  lstrip(...)
 |      S.lstrip([chars]) -> string or unicode
 |      '''和strip类似,不过只去除左边的空格,可以指定字符'''
 |      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
 |  
 |  partition(...)
 |      S.partition(sep) -> (head, sep, tail)
 |      '''和 split 的分隔类似,但返回的是元祖,不过 split 不过保留给定字符,translate 则会保留,并放在中间,没找到则前后为空'''
 |      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.
 |  
 |  replace(...)
 |      S.replace(old, new[, count]) -> string
 |      '''替换,用新的字符串(new),替换原字符串里有的老字符串(old),用 count 指定替换的次数,不指定则全部替换'''
 |      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.
 |  
 |  rfind(...)
 |      S.rfind(sub [,start [,end]]) -> int
 |      '''和S.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.
 |  
 |  rindex(...)
 |      S.rindex(sub [,start [,end]]) -> int
 |      '''和S.index()作用类似,不过寻找方向是从右向左'''
 |      Like S.rfind() but raise ValueError when the substring is not found.
 |  
 |  rjust(...)
 |      S.rjust(width[, fillchar]) -> string
 |      '''右对齐'''
 |      Return S right-justified in a string of length width. Padding is
 |      done using the specified fill character (default is a space)
 |  
 |  rpartition(...)
 |      S.rpartition(sep) -> (head, sep, tail)
 |      '''partition 的右边操作版本'''
 |      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.
 |  
 |  rsplit(...)
 |      S.rsplit([sep [,maxsplit]]) -> list of strings
 |      '''split 的右边操作版,要设置了 maxsplit 才能体现,否则都是全部分隔'''
 |      Return a list of the words in the string 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 or is None, any whitespace string
 |      is a separator.
 |  
 |  rstrip(...)
 |      S.rstrip([chars]) -> string or unicode
 |      '''和strip类似,不过只去除右边的空格,可以指定字符'''
 |      Return a copy of the string S with trailing 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
 |  
 |  split(...)
 |      S.split([sep [,maxsplit]]) -> list of strings
 |      '''按照给定的字符进行分割(从左边开始,找到的第一个),返回一个分割由后剩下的字符串组成的列表(不保留sep),
 |      maxsplit 指定最大分割次数,否则凡是出现指定的分隔符都会分隔'''
 |      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.
 |  
 |  splitlines(...)
 |      S.splitlines(keepends=False) -> 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.
 |  
 |  startswith(...)
 |      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.
 |  
 |  strip(...)
 |      S.strip([chars]) -> string or unicode
 |      '''与 center 的填充相反,这里是移除两边的填充,同样也可以指定移除填充的字符,默认是空格,和 center 类似'''
 |      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.
 |      If chars is unicode, S will be converted to unicode before stripping
 |  
 |  swapcase(...)
 |      S.swapcase() -> string
 |      '''返回一个翻转原字符串字母大小写后的副本'''
 |      Return a copy of the string S with uppercase characters
 |      converted to lowercase and vice versa.
 |  
 |  title(...)
 |      S.title() -> string
 |      '''将字符串转换为标题格式'''
 |      Return a titlecased version of S, i.e. words start with uppercase
 |      characters, all remaining cased characters have lowercase.
 |  
 |  translate(...)
 |      S.translate(table [,deletechars]) -> string
 |      '''根据参数table给出的表(翻译表,翻译表是通过maketrans方法转换而来)转换字符串的字符, 要过滤掉的字符放到 del 参数中'''
 |      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.
 |  
 |  upper(...)
 |      S.upper() -> string
 |      '''将字符串的字母全部大写'''
 |      Return a copy of the string S converted to uppercase.
 |  
 |  zfill(...)
 |      S.zfill(width) -> string
 |      '''返回一个给定长度的字符串(小于原字符串无效),原字符右对齐,前面用0填充'''
 |      Pad a numeric string S with zeros on the left, to fill a field
 |      of the specified width.  The string S is never truncated.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
字符串内置方法

 


   首先,我们要注意一个问题,所以的方法都没有改变原字符串本身,都是返回了一个新对象,具体原理我会在讲到函数中说明,而一个新的对象没有把它赋值给一个变量的话,其引用数就为0,在python进行垃圾回收的时候,就会将其从内存中清除。

   另外,如果你英文够好的话,其实使用 help() 函数就能够自行学习了,所以这里对帮助函数是一些要点进行说明:

  1. <==> 表示相对于,意思这前后的方法效果是一样的

  2. -> 表示函数的返回值,意思是这个方法处理以后,返回的值是什么类型,可以是字符串 string ,也可以是数字 int 等等。关于返回值的详细,会在讲函数的时候分析。

  3.函数进行传值的时候,对传入的值的类型是有要求的,不然会有很多报错,但这里并没有明说一定要传什么类型的值,而已在英文说明中隐含,所以需要一定的英语阅读能力,英文不好就用经验来堆吧。

 


 

   首先,我先来说说对于字符串来说,各运算符的含义:

  1.+

    代表字符串拼接,不多讲了

  2.in

    表示给定的字符串是否在原字符串里面,返回布尔值

  3.==

    判断两个字符串是否一样,值相等就行,返回布尔值

  4.is

    判断是否是统一对象,不仅值要相同,在内存中的地址也有一样,返回布尔值

  5.!=

    不等于,值和对象都不相等,返回布尔值

  5.<,>,<=,>=

    字符串的大小判断非常奇特,它是用每个字符逐一比较,比较的是字符对应的ascll编码,例如:

a = 'a'    #以十进制的ascll为例,其为97
b = 'b'    #以十进制的ascll为例,其为98
a < b

 

  

    另外,其是每个字符逐一比较的,一旦某个字符比另一个大,则整个字符串都大于另一个,例如

    a只有一个字符,但比较的时候,是用 a 的第一个字符 'z' 和 b的第一个字符 'a' 比较,因为'z' > 'a' 了,所以整个字符串都大。如果逐一比较时,两个字符相等的话,就比较下一个字符,如果比较到最后都相等,则说明两个字符串的值相等(==)。至于是否是同一对象就需要另外确定。

  6.*

    字符串的乘法将会返回一个多出重复原字符串的副本。

    只能和数字相乘,字符串间相乘是不可以的。

    也没有什么“乘法分配率”的说法,这样只是重复元祖而已。

  7.%

    取模运算符就是字符串格式化时使用的符号。

 


  关于剩下的内置方法,我会另起一篇进行总结分析。

 

posted @ 2016-05-26 00:15  scolia  阅读(812)  评论(0编辑  收藏  举报