How to Remove Newline Characters from a text File?

When working with text files in Python, you often encounter newline characters (\n) at the end of each line. This article demonstrates how to remove these newline characters using Python's built-in methods.

Let's assume we have a text file called TextFile.txt with the following content:

Good Morning TutorialsPoint
This is TutorialsPoint sample File
Consisting of Specific
source codes in Python,Seaborn,Scala
Summary and Explanation
Welcome TutorialsPoint
Learn with a joy

Approach 1: Using rstrip() with List Comprehension

The most common approach combines readlines() with rstrip() and list comprehension ?

# Create a sample file for demonstration
with open("sample.txt", "w") as f:
    f.write("Good Morning TutorialsPoint\n")
    f.write("This is TutorialsPoint sample File\n")
    f.write("Consisting of Specific\n")
    f.write("source codes in Python,Seaborn,Scala\n")

# Read and remove newlines
with open("sample.txt", 'r') as filedata:
    lines_list = filedata.readlines()
    clean_lines = [line.rstrip('\n') for line in lines_list]
    print(clean_lines)
['Good Morning TutorialsPoint', 'This is TutorialsPoint sample File', 'Consisting of Specific', 'source codes in Python,Seaborn,Scala']

Approach 2: Using read() and splitlines()

Another effective method uses splitlines() which automatically handles newline removal ?

# Using splitlines() method
with open("sample.txt", 'r') as filedata:
    content = filedata.read()
    clean_lines = content.splitlines()
    print(clean_lines)
['Good Morning TutorialsPoint', 'This is TutorialsPoint sample File', 'Consisting of Specific', 'source codes in Python,Seaborn,Scala']

Approach 3: Using strip() for Multiple Whitespace Characters

If your file contains various whitespace characters, strip() removes all trailing whitespace ?

# Create file with different whitespace characters
with open("sample_whitespace.txt", "w") as f:
    f.write("Line with spaces   \n")
    f.write("Line with tabs\t\t\n")
    f.write("Normal line\n")

# Remove all trailing whitespace
with open("sample_whitespace.txt", 'r') as filedata:
    lines_list = filedata.readlines()
    clean_lines = [line.strip() for line in lines_list]
    print(clean_lines)
['Line with spaces', 'Line with tabs', 'Normal line']

Key Methods Explained

  • readlines() Returns a list where each line is a list item with \n at the end

  • rstrip('\n') Removes only newline characters from the right end

  • strip() Removes all whitespace characters from both ends

  • splitlines() Splits text into lines and removes newline characters automatically

Comparison

Method Removes Best For
rstrip('\n') Only newlines Preserving other whitespace
strip() All whitespace Clean text processing
splitlines() All line endings Cross-platform compatibility

Conclusion

Use rstrip('\n') with list comprehension for precise newline removal. For comprehensive whitespace cleaning, use strip(). The splitlines() method offers the cleanest approach for simple line-by-line processing.

Updated on: 2026-03-26T21:21:48+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements