zoukankan      html  css  js  c++  java
  • python strip() 函数探究

    strip()方法语法:str.strip([chars]);

    声明:str为字符串,rm为要删除的字符序列

    • str.strip(rm) 删除字符串中开头、结尾处,位于rm删除序列的字符
    eg1:
    #首尾端'0'被删除,中间不动
    >>> t='0000this is string example0000wow!!!0000'
    >>> t.strip('0')
    'this is string example0000wow!!!'
    
    eg2:
    #无入参,默认删除首尾所有空字符 ‘
    	空格‘
    >>>s='
     0000this is string example0000wow!!!0000
     	'
    >>> s.strip()
    '0000this is string example0000wow!!!0000'
    
    • str.lstrip(rm) 删除字符串中开头处,位于 rm删除序列的字符
    t='0000this is string example0000wow!!!0000'
    >>> t.lstrip('0')
    'this is string example0000wow!!!0000' 
    
    #空入参同样可删除首部空字符,'.rstrip()'同理
    s='
     0000this is string example0000wow!!!0000
     	'
    >>> s.lstrip()
    '0000this is string example0000wow!!!0000
     	'
    
    • str.rstrip(rm) 删除字符串中结尾处,位于 rm删除序列的字符
    t='0000this is string example0000wow!!!0000'
    >>> t.rstrip('0')
    '0000this is string example0000wow!!!'
    
    s='
     0000this is string example0000wow!!!0000
     	'
    >>> s.rstrip()
    '
     0000this is string example0000wow!!!0000'
    

    究竟何为'首尾'?实验之

    s='
     0000this is string is example0000wow!!!0000
     	'
    >>> s.lstrip('
     0')
    'this is string is example0000wow!!!0000
     	'
    #首部'
     0000'被删除
    
    >>> s.lstrip('
     0this')
    'ring is example0000wow!!!0000
     	'
    #奇妙啊,我的目标是删除首部'
     0000this',结果'
     0000this is st'全被删除,说明:符合入参('
     0this')的字符皆是删除对象,不论字符顺序
    #但,为何string后面的is没有删除?因为,'首部'指的是'连续符合'入参要求的字符,string中的'r'隔断了入参的连续字符要求,python判定首部结束。
    

    实验证明:所谓的首、尾,判定依据是-是否连续符合入参要求,如果符合,不论顺序,皆可操作,一直到遇到第一个非入参字符为止.

  • 相关阅读:
    【转】iOS深入学习(Block全面分析)
    iOS—请求Web Service
    iOS设计模式——MVC
    iOS基础知识
    iOS学习——常用博客
    【转】使用segue页面间传递数据
    【转】storyboard之 prepareForSegue:sender:
    【转】NSDictionary和NSMutableDictionary用法详解
    配置.pch文件
    MKNetworkKit下载图片并显示在UIImageView上
  • 原文地址:https://www.cnblogs.com/deepblue775737449/p/8279618.html
Copyright © 2011-2022 走看看