Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str.strip() strange behavior

Tags:

python

string

>>> t1 = "abcd.org.gz"
>>> t1
'abcd.org.gz'
>>> t1.strip("g")
'abcd.org.gz'
>>> t1.strip("gz")
'abcd.org.'
>>> t1.strip(".gz")
'abcd.or'

Why is the 'g' of '.org' gone?

like image 412
Tzury Bar Yochay Avatar asked Jun 07 '10 15:06

Tzury Bar Yochay


4 Answers

strip(".gz") removes any of the characters ., g and z from the beginning and end of the string.

like image 85
Deniz Dogan Avatar answered Oct 13 '22 12:10

Deniz Dogan


x.strip(y) will remove all characters that appear in y from the beginning and end of x.

That means

'foo42'.strip('1234567890') == 'foo'

becuase '4' and '2' both appear in '1234567890'.


Use os.path.splitext if you want to remove the file extension.

>>> import os.path
>>> t1 = "abcd.org.gz"
>>> os.path.splitext(t1)
('abcd.org', '.gz')
like image 39
kennytm Avatar answered Oct 13 '22 11:10

kennytm


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
 
like image 5
Saurav Sahu Avatar answered Oct 13 '22 13:10

Saurav Sahu


The argument given to strip is a set of characters to be removed, not a substring. From the docs:

The chars argument is a string specifying the set of characters to be removed.

like image 1
Will McCutchen Avatar answered Oct 13 '22 12:10

Will McCutchen