stripping spaces in front of line

Steven D'Aprano steve at
Sat Mar 4 05:52:09 CET 2006

On Fri, 03 Mar 2006 20:01:30 -0800, eight02645999 wrote:

> hi
> wish to ask a qns on strip
> i wish to strip all spaces in front of a line (in text file)
> f = open("textfile","rU")
> while (1):
>         line = f.readline().strip()
>         if line == '':
>                 break
>         print line
> f.close()
> in "textfile", i added some spaces in and then ran the code, it prints
> out the lines without the spaces in front. I double checked "textfile"
> and it does contains some lines with spaces in front.
> Is it true that "readline().strip()" perform the removing of spaces in
> front of a line as well? Is it documented anywhere?

As well as what? I don't understand your question. You seem to be asking,
"is it true that strip() strips whitespace just like it is supposed to?".

If so, the answer is yes. 

The strip() method doesn't care whether the string it is called from is a
literal like "  hello world!  ".strip(), or whether it comes from a text
file like f.readline().strip(). All it cares about is that the object is a

You may also find the help() command useful. From an interactive session,
call help(some_object). Remember that functions without the brackets are
objects too: you can say help("".strip).


More information about the Python-list mailing list