# Python Read File to String Strip Newlines

> Read a file with open() and file.read(), then remove newlines with replace() or rebuild the string with splitlines() and join()

**URL:** https://sentry.io/answers/read-a-text-file-into-a-string-and-strip-newlines-in-python/

---

## The Problem

I have a text file (`dna.txt`) containing multiple lines, for example:

```
ATCAGTGGAAACCCAGTGCTA
GAGGATGGAATGACCTTAAAT
CAGGGACGATATTAAACGGAA
```

Using Python, how do I read it into a string variable as one long line, i.e. removing newlines? I want the final string to look like this:

```
ATCAGTGGAAACCCAGTGCTAGAGGATGGAATGACCTTAAATCAGGGACGATATTAAACGGAA
```

## The Solution

We can achieve this using the following Python code:

```python
with open("dna.txt", "r") as file:
    dna = file.read().replace("\n", "")

print(dna)

# will print ATCAGTGGAAACCCAGTGCTAGAGGATGGAATGACCTTAAATCAGGGACGATATTAAACGGAA
```

In the above code:

* [`open("dna.txt", "r")`](https://docs.python.org/3/library/functions.html?#open) opens the file in read mode (`r`). We use Python's [`with` statement](https://docs.python.org/3/reference/compound_stmts.html#with) to automatically close the file at the end of the block.
* [`file.read()`](https://docs.python.org/3/library/io.html#io.TextIOBase.read) reads the entire contents of the file into a string.
* [`replace("\n", "")`](https://docs.python.org/3/library/stdtypes.html?#str.replace) is a string method that replaces all newline characters in our string with empty strings.

In some cases, we may prefer to replace newlines with other characters, such as a single space. We can do this with a slight modification to the above code:

```python
with open("dna.txt", "r") as file:
    dna = file.read().replace("\n", " ") # replace newline with space

print(dna)

# will print ATCAGTGGAAACCCAGTGCTA GAGGATGGAATGACCTTAAAT CAGGGACGATATTAAACGGAA
```

An alternative but less explicit way to produce the same output would be to use [`str.splitlines`](https://docs.python.org/3/library/stdtypes.html#str.splitlines) and [`str.join`](https://docs.python.org/3/library/stdtypes.html#str.join). This will create a list containing each line in the file, and then convert that list into a string with a specified delimiter. We can use an empty string to remove the new lines entirely:

```python
with open("dna.txt", "r") as file:
    dna = "".join(file.read().splitlines())

print(dna)

# will print ATCAGTGGAAACCCAGTGCTAGAGGATGGAATGACCTTAAATCAGGGACGATATTAAACGGAA
```

Alternatively, we could use any other string to separate the lines with that string:

```python
with open("dna.txt", "r") as file:
    dna = " ".join(file.read().splitlines())  # separate lines with a single space

print(dna)

# will print ATCAGTGGAAACCCAGTGCTA GAGGATGGAATGACCTTAAAT CAGGGACGATATTAAACGGAA
```

While both of these approaches produce the same output, the second one may be confusing to readers unfamiliar with Python.

---

*Source: [sentry.io/answers/read-a-text-file-into-a-string-and-strip-newlines-in-python/](https://sentry.io/answers/read-a-text-file-into-a-string-and-strip-newlines-in-python/)*
