programming
newline
string manipulation
coding tips
text processing

How to remove a trailing newline (Python, JavaScript, Bash)

Codemia Knowledge Hub ·

A trailing newline is a newline character at the very end of a string or file. In Python, remove it with text.rstrip('\n'); in JavaScript, text.trimEnd() removes it along with other trailing whitespace.

What counts as a newline

Operating systems encode newlines differently:

  • Unix/Linux: \n (LF - Line Feed)
  • Windows: \r\n (CR+LF - Carriage Return + Line Feed)
  • Mac (pre-OS X): \r (CR - Carriage Return)

When a trailing newline causes problems

  1. Data processing: in CSVs and similar data, a trailing newline can cause errors or unexpected empty lines.
  2. File size: removing unnecessary characters saves a marginal amount of space.
  3. Configuration files: some parsers expect content without a trailing newline.

Python: rstrip('\n')

python
1text = "Example text with newline\n"
2
3# Remove trailing newline
4trimmed = text.rstrip('\n')
5
6print(repr(trimmed))  # Output: 'Example text with newline'

rstrip('\n') strips trailing newline characters only. Use rstrip() with no argument to strip all trailing whitespace.

JavaScript: trimEnd()

javascript
1let text = "Example text with newline\n";
2
3// Remove trailing newline
4let trimmed = text.trimEnd();
5
6console.log(trimmed);  // Logs: 'Example text with newline'

trimEnd() removes all trailing whitespace, including newline characters.

Bash: why the sed and awk one-liners fall short

These commands are often suggested:

bash
echo -n "Example text with newline\n" | sed 's/\n$//'
bash
echo -n "Example text with newline\n" | awk '{sub(/\n$/,"")}1'

They do not do what they appear to. Without -e, echo prints \n as a literal backslash and n, not a newline. And sed and awk process input line by line with the newline already removed, so a pattern like \n$ never sees the line terminator (awk also adds a newline back when it prints). In Bash, command substitution such as "$(cat file)" strips trailing newlines from the captured output.

Language/ToolMethodologySnippet
Pythonrstrip()text.rstrip('\n')
JavaScripttrimEnd()text.trimEnd()
Bashcommand substitutionprintf '%s' "$(< file)"

Before you strip

  • Multiple trailing newlines: rstrip('\n') and trimEnd() remove all of them, not just the last one. Make sure that is what you want.
  • End-of-file newline convention: in version control systems like Git, a newline at the end of a file is usually recommended for compatibility across environments. Consider whether removing it fits that convention.
  • Regular expressions can be tailored when you need more specific newline removal or modification.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design