Read a text file into a string variable and strip newlines in Python
Last Updated :
26 Apr, 2025
It is quite a common requirement for users to remove certain characters from their text files while displaying. This is done to assure that only displayable characters are displayed or data should be displayed in a specific structure. This article will teach you how to read a text file into a string variable and strip newlines using Python.
For demonstration, we would be using the following file:

A file containing 3 sentences in separate lines
Python Read File into String Methods
- Using rstrip() function
- Using replace() function
- Using split() function
- Using splitlines() function
- Using strip function
Python Read file into String using rstrip
We can use rstrip in a List comprehension to read a text file into a string variable and strip newlines in Python. Here is an example of using the rstrip function in Python to read files into string variables and strip newlines.
Python3
with open ( 'text.txt' , 'r' ) as file :
text = " " .join(line.rstrip() for line in file )
print (text)
|
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using Replace() Function
The task could be performed using the replace function, a default function in all Python distributions. Where old is the string to be replaced and new is the string that will replace it. Firstly the path to the file is defined. Then the file’s contents are read and stored in the variable named data. All the occurrences of the newline character in the variable data are replaced with the empty string (nothing). In the end, the data after stripping the newlines are displayed.
Python3
path = r "C:\Users\priviet.txt"
data = open (path, 'r' ).read()
data = data.replace( '\n' , '')
print (data)
|
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using split function
The task could also be performed using the split function, a default function in all Python distributions. The function takes in an argument (optional) a character (or string) and splits the string based on the occurrence of that character in it. Firstly the file is read and stored in a variable as before. Then the variable is passed through the split function, which splits the string and creates a list based on the occurrences of the passed argument. In this case, it was the newline character. Thus after this process, we ended up with a list containing substrings of the original strings.
Python3
path = r "C:\Users\priviet.txt"
data = open (path, 'r' ).read()
data = data.split( "\n" )
data = "".join(data)
print (data)
|
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using Splitlines function
The Splitlines is a function that splits/breaks a string based on the occurrence of escape sequences. The function converts the string to a list upon stripping the control characters. Hence, all the list elements must be iterated to get the final string. This method is almost identical to the split method described earlier. The only difference is that the split lines function does not require any argument and is made to work only with line boundaries. Hence, the splitting of data would be made on any occurrence of a line boundary.
Python3
path = r "C:\Users\priviet.txt"
data = open (path, 'r' ).read()
data = data.splitlines()
data = "".join(data)
print (data)
|
Output
This is fame not clout You don't even know what Rollex Links
Python Read file into String using strip function
The split function could also remove the newline character from a text file. The function optionally takes in argument a separator and strips the leading and trailing separator in the string. Hence, if each line of the text file is passed as an argument and the function is called on the contents of each line, all the newline characters would be stripped from it. Firstly the contents of the text file are read per line, and each line is stored in a list variable.
Python3
path = r "C:\Users\priviet.txt"
data = open (path, 'r' ).readlines()
for x in data:
print (x.strip( '\n' ), end = "")
|
Output
This is fame not clout You don't even know what Rollex Links
Similar Reads
Pulling a random word or string from a line in a text file in Python
File handling in Python is really simple and easy to implement. In order to pull a random word or string from a text file, we will first open the file in read mode and then use the methods in Python's random module to pick a random word. There are various ways to perform this operation: This is the
2 min read
Reading and Writing lists to a file in Python
Reading and writing files is an important functionality in every programming language. Almost every application involves writing and reading operations to and from a file. To enable the reading and writing of files programming languages provide File I/O libraries with inbuilt methods that allow the
5 min read
Read a file without newlines in Python
When working with files in Python, it's common to encounter scenarios where you need to read the file content without including newline characters. Newlines can sometimes interfere with the processing or formatting of the data. In this article, we'll explore different approaches to reading a file wi
2 min read
How to search and replace text in a file in Python ?
In this article, we will learn how we can replace text in a file using python. Method 1: Searching and replacing text without using any external module Let see how we can search and replace text in a text file. First, we create a text file in which we want to search and replace text. Let this file b
5 min read
How to Read Text File Into List in Python?
In this article, we are going to see how to read text files into lists in Python. File for demonstration: Example 1: Converting a text file into a list by splitting the text on the occurrence of '.'. We open the file in reading mode, then read all the text using the read() and store it into a variab
2 min read
How to Split a File into a List in Python
In this article, we are going to see how to Split a File into a List in Python. When we want each line of the file to be listed at consecutive positions where each line becomes an element in the file, the splitlines() or rstrip() method is used to split a file into a list. Let's see a few examples t
5 min read
Reading and Writing to text files in Python
Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character called EOL
8 min read
Find line number of a specific string or substring or word from a .txt file in Python
Finding the line number of a specific string and its substring is a common operation performed by text editors or any application with some level of text processing capabilities. In this article, you will learn how to find line number of a specific string or substring or word from a .txt (plain tex
4 min read
Python - How to search for a string in text files?
In this article, we are going to see how to search for a string in text files using Python Example: string = "GEEK FOR GEEKS"Input: "FOR" Output: Yes, FOR is present in the given string. Text File for demonstration: Finding the index of the string in the text file using readline() In this method, we
2 min read
Take input from user and store in .txt file in Python
In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let's see the implementation below. Stepwise Implementation Ste
2 min read