csev/py4e

PY4E - Regular Expressions -- use of rstrip() should be lstrip()

timmid1 opened this issue · 3 comments

Throughout this chapter (and I recall seeing the same error in prior chapters) the example codes all use rstrip() which makes no sense for an expression searching from the beginning of the string. These should be changed to lstrip().

Wrong! rstrip() removes the hard return at the end of each line, so that the output is not double spaced. But query whether the programs would be more efficient if the "line = line.rstrip()" was moved down two lines to immediately before, or better as a part of, the print statement. i.e. make the print statement: "print(line.rstrip())".

csev commented

The use of rstrip() is to remove tailing whitespace very much on purpose. Sometimes leading whiespace has meaning. The pattern of read a line and strip trailing space is used throughout. Make it appear that lstrip should always be used is misleading.

csev commented

Understanding is more important than efficiency when writing programs of 10 lines or less.