How to read a text file into a string variable and strip newlines?

Each Answer to this Q is separated by one/two green lines.

I use the following code segment to read a file in python:

with open ("data.txt", "r") as myfile:

Input file is:


and when I print data I get


As I see data is in list form. How do I make it string? And also how do I remove the "\n", "[", and "]" characters from it?

You could use:

with open('data.txt', 'r') as file:
    data ='\n', '')

Or if the file content is guaranteed to be one-line

with open('data.txt', 'r') as file:
    data =

In Python 3.5 or later, using pathlib you can copy text file contents into a variable and close the file in one line:

from pathlib import Path
txt = Path('data.txt').read_text()

and then you can use str.replace to remove the newlines:

txt = txt.replace('\n', '')

You can read from a file in one line:

str = open('very_Important.txt', 'r').read()

Please note that this does not close the file explicitly.

CPython will close the file when it exits as part of the garbage collection.

But other python implementations won’t. To write portable code, it is better to use with or close the file explicitly. Short is not always better. See

To join all lines into a string and remove new lines, I normally use :

with open('t.txt') as f:
  s = " ".join([l.rstrip() for l in f]) 

with open("data.txt") as myfile:
    data="".join(line.rstrip() for line in myfile)

join() will join a list of strings, and rstrip() with no arguments will trim whitespace, including newlines, from the end of strings.

This can be done using the read() method :

text_as_string = open('Your_Text_File.txt', 'r').read()

Or as the default mode itself is ‘r’ (read) so simply use,

text_as_string = open('Your_Text_File.txt').read()

I’m surprised nobody mentioned splitlines() yet.

with open ("data.txt", "r") as myfile:
    data =

Variable data is now a list that looks like this when printed:


Note there are no newlines (\n).

At that point, it sounds like you want to print back the lines to console, which you can achieve with a for loop:

for line in data:

I have fiddled around with this for a while and have prefer to use use read in combination with rstrip. Without rstrip("\n"), Python adds a newline to the end of the string, which in most cases is not very useful.

with open("myfile.txt") as f:
    file_content ="\n")

It’s hard to tell exactly what you’re after, but something like this should get you started:

with open ("data.txt", "r") as myfile:
    data=" ".join([line.replace('\n', '') for line in myfile.readlines()])

you can compress this into one into two lines of code!!!

content = open('filepath','r').read().replace('\n',' ')

if your file reads:

hello how are you?
who are you?
blank blank

python output

hello how are you? who are you? blank blank

You can also strip each line and concatenate into a final string.

myfile = open("data.txt","r")
data = ""
lines = myfile.readlines()
for line in lines:
    data = data + line.strip();

This would also work out just fine.

This is a one line, copy-pasteable solution that also closes the file object:

_ = open('data.txt', 'r'); data =; _.close()

python3: Google “list comprehension” if the square bracket syntax is new to you.

 with open('data.txt') as f:
     lines = [ line.strip('\n') for line in list(f) ]


  • List: "".join([line.rstrip('\n') for line in open('file.txt')])

  • Generator: "".join((line.rstrip('\n') for line in open('file.txt')))

List is faster than generator but heavier on memory. Generators are slower than lists and is lighter for memory like iterating over lines. In case of “”.join(), I think both should work well. .join() function should be removed to get list or generator respectively.

  • Note: close() / closing of file descriptor probably not needed

Have you tried this?

x = "yourfilename.txt"
y = open(x, 'r').read()


To remove line breaks using Python you can use replace function of a string.

This example removes all 3 types of line breaks:

my_string = open('lala.json').read()

my_string = my_string.replace("\r","").replace("\n","")

Example file is:

  "lala": "lulu",
  "foo": "bar"

You can try it using this replay scenario:

enter image description here

f = open('data.txt','r')
string = ""
while 1:
    line = f.readline()
    if not line:break
    string += line



I don’t feel that anyone addressed the [ ] part of your question. When you read each line into your variable, because there were multiple lines before you replaced the \n with ” you ended up creating a list. If you have a variable of x and print it out just by


or print(x)

or str(x)

You will see the entire list with the brackets. If you call each element of the (array of sorts)

then it omits the brackets. If you use the str() function you will see just the data and not the ” either.

Maybe you could try this? I use this in my programs.

Data= open ('data.txt', 'r')
data = Data.readlines()
for i in range(len(data)):
    data[i] = data[i].strip()+ ' '

Regular expression works too:

import re
with open("depression.txt") as f:
     l = re.split(' ', re.sub('\n',' ',[:-1]

print (l)

[‘I’, ‘feel’, ’empty’, ‘and’, ‘dead’, ‘inside’]

with open('data.txt', 'r') as file:
    data = [line.strip('\n') for line in file.readlines()]

This works:
Change your file to:



file = open("file.txt")
line =
words = line.split()

This creates a list named words that equals:


That got rid of the “\n”. To answer the part about the brackets getting in your way, just do this:

for word in words: # Assuming words is the list above
    print word # Prints each word in file on a different line


print words[0] + ",", words[1] # Note that the "+" symbol indicates no spaces
#The comma not in parentheses indicates a space

This returns:


with open(player_name, 'r') as myfile:
 list=data.split(" ")

This code will help you to read the first line and then using the list and split option you can convert the first line word separated by space to be stored in a list.

Than you can easily access any word, or even store it in a string.

You can also do the same thing with using a for loop.

file = open("myfile.txt", "r")
lines = file.readlines()
str=""                                     #string declaration

for i in range(len(lines)):
    str += lines[i].rstrip('\n') + ' '

print str

Try the following:

with open('data.txt', 'r') as myfile:
    data =

    sentences = data.split('\\n')
    for sentence in sentences:

Caution: It does not remove the \n. It is just for viewing the text as if there were no \n

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.