Strange behaviour with a for loop.

Larry Hudson orgnut at yahoo.com
Sat Jan 4 17:20:10 EST 2014


On 01/03/2014 10:32 PM, Sean Murphy wrote:
> Hi everyone.
[snip]
> The 2nd part of my original question still stands. I will expand upon this a bit more to give more context. I want to print from the beginning of the paragraph to the end. Each paragraph ends with "\n\n\n".
>
> If I use "\n\n\n" in lines this does return true for the string. But I don't have a starting position and ending position. The list method which I mention before can be sliced by going back one element.
>
> Any suggestion on this would be welcomed. I want to achieve this using standard core python objects/methods.
>
Another useful string method is endswith().  With that you don't need to know the line length:

if line.endswith('\n\n\n'):
     ...

(Of course, there is a corresponding startswith() method also.)

If you are specifically looking for blank lines, someone already suggested isspace().  Another 
possibility is rstrip(), which will remove all trailing whitespace.  So you can check for blank 
lines with:

if line.rstrip() == '':
     ...

There are three of these:
lstrip() is left-strip, which removes leading whitespace,
rstrip() is right-strip, which removes trailing whitespace, and
strip() which removes whitespace from both ends.

All of these are very useful functions.

      -=- Larry -=-




More information about the Python-list mailing list