str.strip() strange behavior

前端 未结 5 730
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 08:25
>>> t1 = "abcd.org.gz"
>>> t1
\'abcd.org.gz\'
>>> t1.strip("g")
\'abcd.org.gz\'
>>> t1.strip("gz")
         


        
5条回答
  •  伪装坚强ぢ
    2020-11-27 09:17

    In Python 3.9, there are two new string methods .removeprefix() and .removesuffix() to remove the beginning or end of a string, respectively. Thankfully this time, the method names make it aptly clear what these methods are supposed to perform.

    >>> print (sys.version)
    3.9.0 
    >>> t1 = "abcd.org.gz"
    >>> t1.removesuffix('gz')
    'abcd.org.'
    >>> t1
    'abcd.org.gz'
    >>> t1.removesuffix('gz').removesuffix('.gz')
    'abcd.org.'     # No unexpected effect from last removesuffix call
     
    

提交回复
热议问题