Python之字符串方法

def capitalize(self): # 第一个字符变大写

def center(self, width, fillchar=None): # 内容居中,两端可指定内容填充

def count(self, sub, start=None, end=None): # 返回字符出现次数

def endswith(self, suffix, start=None, end=None): # 判断以什么结尾是否为真

def expandtabs(self, tabsize=8): # 把字符串中tab换成n个空格

def find(self, sub, start=None, end=None): #查找子序列的位置,没有返回-1
def format(self, *args, **kwargs): #字符串的格式化,动态参数   
example:
  test = 'hello {0},age{1}'

  res = test.format('chenyan','19')

  print(res)

    hello chenyan,age19    #返回结果
def index(self, sub, start=None, end=None): #字符串子序列位置,没找到会报错
def isalnum(self): #判断是否所有字符都是字母数字
def isalpha(self): #判断字符串是否全部为字母
def isdecimal(self): #判断字符串没有小数点为true
def isdigit(self): #判断字符串是否都是数字    
def isidentifier(self): # 判断字符串是否有效的标识符,不是为真

def islower(self): # 判断字符串是否全部为小写

def isnumeric(self): # 判断字符串是否全部为数字

def isspace(self): # 字符串全部为空格为真

def istitle(self): #判断是否为标题(单词首字母大写)
def isupper(self): #判断是否全部为大写
def join(self, iterable): #返回一个字符串,它是字符串的连接
example:
  i = ['hehe','haha']
  res = '-'.join(i)
  print(res)
    hehe-haha  #返回结果
def ljust(self, width, fillchar=None): #返回的是字符串长度宽度的左对齐。右侧填充(默认是空格)
example:
  i = 'hehe'

  res = i.ljust(20,'*')

  print(res)

    hehe****************   #返回
def lower(self): #可以让字符串变小写
def lstrip(self, chars=None): #移除字符串左边的空格
def partition(self, sep): #sep为指定分隔符

example:
   i = 'hehe&haha'

    res = i.partition('&')

    print(res)

      ('hehe', '&', 'haha') 返回元组

def replace(self, old, new, count=None): # 替换,可指定替换个数
example:
    i = 'hehe&haha'

    res = i.replace('he','He',1)

      print(res)
def rjust(self, width, fillchar=None): # 右边对齐,左边填充
def rpartition(self, sep): #指定字符串分隔符后,分前中后三部分
def rsplit(self, sep=None, maxsplit=-1): #返回一个单词列表,使用sep作为分隔符字符串
def rstrip(self, chars=None): # 去除右边的空格

def split(self, sep=None, maxsplit=-1): # 指定分隔符分割

def splitlines(self, keepends=None): #根据换行符分割
def startswith(self, prefix, start=None, end=None): # 判断前缀

def strip(self, chars=None): # 去除首尾的空格

def swapcase(self): # 可以把字符串大小写反向转换

def title(self): # 可以把字符串的单词首字符变大写,其他变小写

def upper(self): # 转换大写

def zfill(self, width): #在左侧填充一个数字字符串,右边对齐
example:
  i = 'my love'

  res = i.zfill(12)

  print(res)

    00000my love  #返回结果
 
上一篇:指定的 CGI 应用程序遇到错误,服务器终止了该进程。


下一篇:BootStrapt iCheck表单美化插件使用方法详解(含参数、事件等) 全选 反选