zoukankan      html  css  js  c++  java
  • python字符串的方法

    ##############################################################################
    center(self, width, fillchar=None)  内容居中,width:总长度;fillchar:空白处填充内容,默认无
    a = 'eason'
    b = a.center(20,'#')
    print(b)
    		运行结果:#######eason########
    ##############################################################################
    
    a = 'eason is good a boy '
    b = a.count('a')
    print(b)
    c = a.count('a',0,14)
    print(c)
    ## 其中 0,14表示a的范围,0即表示第1个字母a.
    		运行结果:2
    				  1
    
    ##############################################################################
    
    
    #(endswith)判断字符串是以什么结尾,如果是返回True,否则返回False.
    a = 'eason'
    b = a.endswith('n')
    c = a.endswith('a',0,2)
    print(b)
    print(c)  
    		运行结果:True 
    				  True
    	
    			  
    ##############################################################################
    #(expandtabs) 将制表符换成空格,默认为8个空格。
    
    a = 'eason	 is boy'
    b=a.expandtabs(9)
    print(b)
    
    
    ##############################################################################
    (find) 寻找子序列的位置,如果没找到,则返回-1.
    a = 'eason'
    b = a.find('n')
    c= a.find('e')
    d= a.find('a',0,1)
    print(b)
    print(c)
    print(d)
    		运行结果:	
    			4
    			-1
    			-1
    	
    ##############################################################################		
    (format) 字符串格式化,动态参数。其中{0},{1}为占符
    a = "name,{0},age,{1}"
    print(a)
    b =a.format('eason',18)
    print(b)
    		运行结果:a = "name,{0},age,{1}"
    
    
    ##############################################################################
    
    (index) 子序列的位置,如果没找到,报错。
    a = 'eason'
    b = a.index('a')
    print(b)
    		运行结果:1
    		
    添加一个找不到的字符
    a = 'eason'
    b = a.index('f')
    print(b)
    				
    		运行结果:
    				b = a.index('f')
    				ValueError: substring not found
    
    #这个和find的非常相似,只是find的报错形式是输出-1.
    
    ##############################################################################
    (isalnum) 是否是字母和数字,符合其中一个即可,返回结果bool的形式输出。
    
    a = 'eason123'
    b = a.isalnum()
    print(b)		
    		
    		运行的结果:True
    
    
    ##############################################################################	
    (isalpha) 是否只含有字母,返回结果以bool的形式输出。
    a = 'eason'
    b = a.isalpha()
    c = 'eason123'
    d = c.isalpha()
    print(b)
    print(d)
    		运行的结果:True
    					False
    					
    
    ##############################################################################
    (isdigit) 是否是数字,返回结果以bool的形式输出。
    
    a = '123'
    b = a.isdigit()
    print(b)
    		运行的结果:True
    
    ##############################################################################
    
    (islower) 是否小写
    
    a = 'eason'
    b = a.islower()
    print(b)
    		运行的结果:True
    		
    ##############################################################################
    (isspace)  测字符串是否只由空格组成
     a = ' '
     b=a.isspace()
     print(b)
    		运行结果:True
    		
    ##############################################################################	
    istitle  判断每个字符串第一个字母是不是大写,其他是不是小写。
    
    a = 'Hello,Boy'
    b = a.istitle()
    print(b)
    		运行结果:True	
    
    	
    ##############################################################################
    join(self, iterable) 迭代连接
    a = [ 'eason','boy']   #a表示的是列表
    b = "_".join(a)
    print(b)
    一般是将列表中的内容连接起来。python3中如果列表中是数字,不能直接连接,需要加上单引号作为字符串处理,python2是可以的。
    
    ##############################################################################
    isupper(是否全是大写)
    a = 'EASON'
    b=a.isupper()
    print(b)
    		运行结果:True
    
    ##############################################################################
    (self, width, fillchar=None)   内容左对齐,右侧填充。
    width -- 指定字符串长度。
    fillchar -- 填充字符,默认为空格。
    
    a = 'eason'
    b = a.ljust(20,'#')
    print(b)
    		运行结果:eason###############		
    
    ##############################################################################
    lower(大小变小写)
    
    a  = 'EASON'
    b = a.lower()
    print(b)
    
    ##############################################################################
    
    partition(self, sep) 分割切前中后三部分
    
    a= 'eason'
    b=a.partition('s')
    print(b)
    		运行结果:('ea', 's', 'on')
    
    ##############################################################################
    
    replace(self, old, new, count=None)	 替换count指的是替换的次数
    
    a = 'easonoo'
    b = a.replace('o','O',3)
    print(b)
    
    ##############################################################################
    split(self, sep=None, maxsplit=None) 分割,以什么为分割符,maxsplit最多分割几次 
    
    a = 'easonobocod'
    b=a.split('o',3)
    print(b)
    
    ##############################################################################	
    splitlines(self,keepends=False)"根据换行分割"
    a = 'eason
    eason2'
    ret = a.splitlines()
    print(ret)
    		运行结果:
    
    ##############################################################################	
    
    startswith(self, prefix, start=None, end=None) 以什么开始,以bool值返回结果。
    
    a = 'eason'
    b = a.startswith('ea')
    print(b)
    
    ##############################################################################	
    strip(self, chars=None)  #返回字符串并移除首尾字符,默认空白;
    a = '  eason  '
    b = a.strip()
    print(b)
    			运行结果:eason
    			
    a = 'abceason  '
    b = a.strip('abc')
    print(b)    运行结果:eason
    
    ##############################################################################	
    lstrip(self, chars=None) #移除左侧空白
    
    a = '  eason'
    b = a.lstrip()
    print(b)
    			运行结果:eason  
    			
    ##############################################################################				
    rstrip(self, chars=None)	
    
    a = 'eason  '
    b = a.rstrip()
    print(b)
    			运行结果:eason  
    			
    ##############################################################################				
    swapcase  大写变小写,小写变大写
    a = 'eaSon'
    b = a.swapcase()
    print(b)
    
    ##############################################################################	
    upper 小写变大写
    a = 'eason'
    b = a.upper()
    print(b)
    ##############################################################################	
    
    rfind(find返回的是匹配的第一个字符串的位置,而rfind返回的是匹配的最后一个字符串的位置)
    str = 'eason boy'
    ret = str.find('o')
    ret2 = str.rfind('o')
    print(ret)
    print(ret2)
    		运行结果:3
    				  7
    ##############################################################################	
    rindex(self, sub, start=None, end=None)  (index 寻找子序列的位置,如果没找到,则返回-1,rindex返回匹配的最后一个字符的位置)
    
    a = 'eason boy'
    ret = a.rindex('o')
    print(ret)
    		运行结果:7
    ##############################################################################		
    rjust(self, width, fillchar=None) (右对齐,左边补充)
    a = 'eason'
    ret = a.rjust(20,'#')
    print(ret)
    		运行结果:###############eason
    		
    ##############################################################################			
    rpartition(self, sep) 分割切前中后三部分,类似于 partition()函数,不过是从右边开始查找.
    
    a = 'eason is good  is boy '
    ret = a.rpartition('is')
    print(ret)	
    		运行结果:('eason is good  ', 'is', ' boy ')
    		
    ##############################################################################	
    rsplit(self, sep=None, maxsplit=None) 切割,从右边开始切割。
    
    a = 'eason  is boy2 '
    ret = a.rsplit('is',)
    print(ret)
    		运行结果:['eason  ', ' boy2 ']
    		
    
    ##############################################################################		
    zfill(self, width)  指定返回的字符串的长度,原字符串右对齐,前面填充0。
    a = 'eason'
    ret = a.zfill(10)
    print(ret)
    		运行结果:00000eason

    字符串额外补充常用的功能:

    
    
    
    索引:
    a = 'eason'
    获取第一字符:a[0]
    获取第二个字符:a[1]
        .
        .
        .
    获取第:5个字符:a[4]
    print(a[0])
    print(a[1])
    print(a[2])
    print(a[3])
    print(a[4])
    
    
    如果a的字符串过程,但是为显示每个元素,所以不太方便用a[0] a[1]...这种方式,所以使用for循环。
    for i in a:
        print(i)     //和shell中的for循环基本类似,当然for循环也是你可以正常使用continue  break的。    
    或者或者使用while循环
    a = 'eason'
    start = 0
    while start < len(a):
            print(a[start])
            start += 1 
    
    计算字符串长度可以使用len()函数。
        
    
    
    切片:
    其中0:2表示大于等于0小于2.
    
    a = 'eason'
    ret = a[0:2]
    print(ret) 
    
    
  • 相关阅读:
    分治法解决寻找数组中最大最小值的问题
    bootstrap动画进度条
    关于bootstrap中css样式与自己设置的css样式相撞问题
    css引入外部字体
    清除浮动
    四叶玫瑰数
    水仙花数
    nginx 配置文件服务器
    springboot 自定义拦截器 防止恶意请求
    springboot 自定义异常
  • 原文地址:https://www.cnblogs.com/lin1/p/7440155.html
Copyright © 2011-2022 走看看