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
- Data processing: in CSVs and similar data, a trailing newline can cause errors or unexpected empty lines.
- File size: removing unnecessary characters saves a marginal amount of space.
- Configuration files: some parsers expect content without a trailing newline.
Python: rstrip('\n')
rstrip('\n') strips trailing newline characters only. Use rstrip() with no argument to strip all trailing whitespace.
JavaScript: trimEnd()
trimEnd() removes all trailing whitespace, including newline characters.
Bash: why the sed and awk one-liners fall short
These commands are often suggested:
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/Tool | Methodology | Snippet |
| Python | rstrip() | text.rstrip('\n') |
| JavaScript | trimEnd() | text.trimEnd() |
| Bash | command substitution | printf '%s' "$(< file)" |
Before you strip
- Multiple trailing newlines:
rstrip('\n')andtrimEnd()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.
Related reading
- How can I run another application within a panel of my C program?
- How can I speed up a topic model in R?
- How can I split a text into sentences?
- How can I split and trim a string into parts all on one line?
- How can I trim beginning and ending double quotes from a string?
- How do document diff algorithms work?
- How do I approximate Did you mean? without using Google?
- How do I determine the longest similar portion of several strings?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.