码迷,mamicode.com
首页 > 其他好文 > 详细

字符串常用函数

时间:2017-11-17 00:13:52      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:eric   specified   查询   containe   rest   出现   convert   padding   指定   

     1、count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__

   ---取字符串当中 字符出现的次数,start,end 默认为空 表示字符从头到尾。 start是从第几个字符开始,第一字符位是0.
S.count(sub[, start[, end]]) -> int
例如:
      i1 = "xuexipython"
      print (i1.count(‘x‘))
         ----“xuexipython” 当中  字符‘x’ 出现的次数  为2次。
 
print (i1.count(‘x‘,1,20))

      ----“xuexipython” 当中 从u字符开始,字符‘x’ 出现的次数 为1次。

 2、capitalize(self): # real signature unknown; restored from __doc__
    """
S.capitalize() -> string
---字符串首字母转换为大写
Return a copy of the string S with only its first character
capitalized.
例如:
    i1 = "xuexipython"

    print (i1.capitalize())
    ---结果为:“Xuexipython”
3、center(self, width, fillchar=None): # real signature unknown; restored from __doc__
    """指定字符串长度   然后字符居中 默认用空格填充 fillchar = none ,也可以用指定字符来填充
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)
   例如:
    i1 = "xuexipython"

    i2 = i1.center(19,‘#‘)

    print (i2)
    ---####xuexipython#### ----用“#”来填充   
4、
upper(self): # real signature unknown; restored from __doc__
    """ 把字符串转换为大写
S.upper() -> string

Return a copy of the string S converted to uppercase.
5、
lower(self): # real signature unknown; restored from __doc__
    """把字符串转换为小写
S.lower() -> string

Return a copy of the string S converted to lowercase.
6、index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """ 查找字符串中,指定字符开始的位置,star,end 默认值为空,在整个字符串中进行查找,也可指定字符中起始位置。
功能类似于:
find
      区别在于index 在查找时,查询不到结果时报错,ValueError: substring not found
find 在查找时,查询不到结果时不会报错,‘-1’
    S.index(sub [,start [,end]]) -> int

Like S.find() but raise ValueError when the substring is not found.
    例如:
    i1 = "xuexipython"
    print (i1.index(‘xi‘))
----‘xi’字符在 "xuexipython"中在第几位置,3
7、
find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
    """查找字符串中,指定字符开始的位置,star,end 默认值为空,在整个字符串中进行查找,也可指定字符中起始位置。
    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.
8、endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
    """ endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

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.
例如:
    i1 = "xuexipython"

    print (i1.endswith(‘n‘))
返回值:true
    print (i1.endswith(‘i‘,0,5))
    返回值:true
9、format(self, *args, **kwargs): # known special case of str.format
    """格式化   只能从0开始
S.format(*args, **kwargs) -> string

Return a formatted version of S, using substitutions from args and kwargs.
The substitutions are identified by braces (‘{‘ and ‘}‘).
  例如:
    i1 = "xuexipython,{0},{1}"

    print (i1.format(‘age‘,‘tt‘))
    返回值:xuexipython,age,tt

10、isalnum(self): # real signature unknown; restored from __doc__
    """是判断 self 是否有字符或数字 
S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
    例如:
    i1 = "xuexipython"
    i2 = "test230"
    i3 = " "
    print (i1.isalnum())
    print (i2.isalnum())
    print (i3.isalnum())
    返回值:

      True
      True
      False

11、isalpha(self): # real signature unknown; restored from __doc__

    """判断self中的值是否全为字符
S.isalpha() -> bool

Return True if all characters in S are alphabetic
and there is at least one character in S, False otherwise.
例如:
    i1 = "xuexipython"
    i2 = "test230"
    i3 = " "

    print (i1.isalpha())
    print (i2.isalpha())
    print (i3.isalpha())
      返回值:

    True
    False
    False

12、isspace(self): # real signature unknown; restored from __doc__

    """ 判断self字符串是不是都为空格,
S.isspace() -> bool

Return True if all characters in S are whitespace
and there is at least one character in S, False otherwise.
    例如:
    i1 = "xuexipython "
    i2 = " test230"
    i3 = " "
    print (i1.isspace())
    print (i2.isspace())
    print (i3.isspace())
    返回值:

    False
    False
    True

13、  lstrip    str.lstrip([chars])

   lstrip() 方法用于截掉字符串左边的空格或指定字符,或指定的字符串
例如:
    i1 = "xuexipython"

    print (i1.lstrip(‘x‘))
    返回:uexipython
14、 rstrip    str.rstrip([chars])
   lstrip() 方法用于截掉字符串左边的空格或指定字符,或指定的字符串
例如:
    i1 = "xuexipython"

    print (i1.rstrip(‘n‘))
    返回:xuexipytho
15、strip  str.strip([chars])
strip() 方法用于截掉字符串两边的空格。

16、ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
    """字符靠左对齐,不够补齐,默认为空格
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).
    例如:
  i1 = "xuexipython"

  print (i1.ljust(20,‘#‘))
  返回:xuexipython#########
17、rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
    """字符靠右对齐,不够补齐,默认为空格,同ljust正好相反
    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)

      

    

 

 




字符串常用函数

标签:eric   specified   查询   containe   rest   出现   convert   padding   指定   

原文地址:http://www.cnblogs.com/ylpython/p/7835034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!