Removing a string prefix in Python
A little Python thing I like to do, but have never seen in other people's code is to remove a prefix like this:
s = 'some string' if s.startswith('some '): s = s[len('some '):]
I like the s[len('some '):] approach as I find it both error-robust (as opposed to typing the actual number like s[5:]) and self-documenting. For example, consider:
from glob import glob files = glob('datadir/experiment/*.txt') ids = [f[len('datadir/'):] for f in files] It is pretty clear that what I want to do is remove the datadir/ prefix. It works for suffixes too: without_ext = filename[:-len('.txt')] combined = filename[len('datadir/experiment/'):-len('.txt')] This is much better than [1]: combined = filename[18:-4] § (One may be tempted to write filename.replace('.txt','') to get rid of a suffix, but this is wrong! It does not work with 'datadir/experiments/datafiles.txt/filename.txt', which is perfectly legal.) § It is slightly inefficient because the Python interpreter will actually create a string, then compute its length. [2] However, this is generally in code where it does not matter that much. If it did, I'd be doing it in C(++) or using some other method. [1] It should have been filename[19:-4], but it's hard to see immediately. In any case, writing a number always makes me think and code should not make you think too muchg [2] It is not allowed to just replace it by the result statically because you may have redefined the function len. It could have a check for the common case, I suppose.