file.readlines() leaving blank lines
Question: I have read the `file.readlines()` reads the whole file line by line and
stores it in a list. If I have a file like so -

    
    
    Sentence 1
    Sentence 2
    Sentence 3
    

and I use `.readlines()` to print each sentence like so -

    
    
    file = open("test.txt") 
    for i in file.readlines():
        print i
        enter code here
    

The output is

    
    
    Sentence 1
    
    Sentence 2
    
    Sentence 3
    

My question is why do I get the extra line between each sentence and how can I
get rid of it ? Please excuse me if this question is pretty stupid but I am
not very experienced in Python.

**UPDATE**

I found that using `i.strip()` also removes the extra lines , why does this
happen ? As far as I know, `.split()` removes the white spaces at the end and
beginning of a string.


Answer: `file.readlines()` return list of strings. Each string contain trailing
newlines. `print` statement prints the passed parameter with newlnie.; That's
why you got extra lines.

To remove extra newline, use `str.rstrip`:

    
    
    print i.rstrip('\n')
    

or use `sys.stdout.write`

    
    
    sys.stdout.write(i)
    

BTW, don't use `file.readlines` unless you need all lines at once. Just
iterate the file.

    
    
    with open("test.txt") as f:
        for i in f:
            print i.rstrip('\n')
            ...
    

**UPDATE**

In Python 3, to prevent `print` prints trailing newline, you can use `print(i,
end='')`.

In Python 2, you can use same feature if you do : [`from __future__ import
print_function`](http://docs.python.org/2/library/__future__.html)

**Answer to UPDATE**

Tabs, Newlines are also considers as whitespaces.

    
    
    >> ' \r\n\t\v'.isspace()
    True
    

