Read a text file into a string variable and strip newlines in Python
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
Strip newlines 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.
Example:
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
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # The file is read and its data is stored data = open (path, 'r' ).read() # Replacing all occurrences of newline in data with '' data = data.replace( '\n' , '') # Displaying the resultant data print (data) |
Output:
This is fame not clout You don't even know what Rollex Links
Strip newlines 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.
Example:
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. After which, all the list elements are joined using the join function, which produces a string. In the end, the joined string is displayed.
Python3
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # The file is read and its data is stored data = open (path, 'r' ).read() # We splitted the string based on the # occurrence of newline character # A new list is created where newline # character is not present in any element data = data.split( "\n" ) # Joined all the elements of the list to # form a single string data = "".join(data) # Displaying the resultant data print (data) |
Output:
This is fame not clout You don't even know what Rollex Links
Strip newlines 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.
Example:
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
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # The file is read and its data is stored data = open (path, 'r' ).read() # We splitted the string based on the occurrence of line boundaries # A new list is created where newline character is not present in any element data = data.splitlines() # Joined all the elements of the list to form a single string data = "".join(data) # Displaying the resultant data print (data) |
Output:
This is fame not clout You don't even know what Rollex Links
Strip newlines 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.
Example:
Firstly the contents of the text file are read per line, and each line is stored in a list variable. Then all the elements of the list variable are iterated over. In each iteration, a line is processed, trailing newlines are stripped from the lines (if present), and the line is displayed. *end=”” argument is passed to the print function to remove the automatic newline produced by the print function on each call
Python3
# Variable contains the path to the file path = r "C:\Users\priviet.txt" # Each line of the file is read, and # stored in a list variable data = open (path, 'r' ).readlines() # The loop iterates over each line of the file for x in data: # Stripping the newline character from each line # and displaying it sequentially. print (x.strip( '\n' ), end = "") |
Output:
This is fame not clout You don't even know what Rollex Links
Strip newlines using rstrip
We can use rstrip in a List comprehension to read a text file into a string variable and strip newlines in Python.
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
Please Login to comment...