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

Python str.strip weird behavior

>>> 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 has' g '.org' gone?

+4
source share
4 answers

strip removes any characters . , g and z from the beginning and end of the line.

+8
source

x.strip(y) will delete all characters that appear in y , starting at the beginning and end of x .

It means

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

becuase '4' and '2' displayed 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') 
+8
source

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

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

+1
source

as far as I know, strip only deletes the beginning or end of a line. If you want to remove from the entire line, replace it.

+1
source

Source: https://habr.com/ru/post/1312101/


All Articles