To remove literal carriage-return and line-feed characters anywhere in a Python string, use text.replace("r", "").replace("n", ""). Removing only n can leave r behind when the input uses Windows-style CRLF line endings (rn). If you want to remove a broader range of line boundaries, use "".join(text.splitlines()).
Choose the cleanup that matches your goal
| Goal | Python | What it removes |
|---|---|---|
| Remove line feeds only | text.replace("n", "") |
Every literal LF character (n) |
| Remove carriage returns only | text.replace("r", "") |
Every literal CR character (r) |
| Remove CR and LF wherever they occur | text.replace("r", "").replace("n", "") |
Every CR and LF character, including both characters in CRLF |
Remove line boundaries recognized by splitlines() |
"".join(text.splitlines()) |
CR, LF, CRLF, and additional control-character and Unicode line boundaries |
| Replace recognized line boundaries with spaces | " ".join(text.splitlines()) |
Recognized boundaries become spaces instead of disappearing |
For literal character removal, str.replace(old, new) returns a copy with all occurrences replaced when no count is supplied. Python strings are immutable, so keep the returned string—typically by assigning it to a variable. See the Python 3.14 built-in types documentation.
Why deleting n can leave r
CR (r) and LF (n) are separate characters. A CRLF line ending contains both, in that order. Therefore text.replace("n", "") removes the LF but leaves the CR. If you need to delete both characters regardless of whether they occur alone or as CRLF, replace both, as shown above.
If you know the string contains only CRLF line endings and want to remove that exact two-character sequence, you can use text.replace("rn", ""). That does not remove standalone CR or LF characters, so use it only when the input format is known.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Repair Windows errors before they cause bigger problemsFix Now →#1 Best Overall
When splitlines() is the better fit
splitlines() separates a string at recognized line boundaries and, by default, returns the pieces without those boundaries. Joining the pieces with an empty string removes the boundaries; joining with a space preserves a visible gap between adjacent lines. This matters when deleting a break would otherwise run words together.
The method recognizes more than CR, LF, and CRLF: it also handles vertical tab, form feed, several control-character separators, and Unicode next-line, line-separator, and paragraph-separator characters. Use it when that broader behavior is wanted; use replace() when you intend to remove only particular literal characters.
Rank #2
There are also small differences in how it treats empty input and a trailing line break: "".splitlines() returns an empty list, and a terminal line break does not create an extra final item. That differs from split("n") in those cases. Details are in the official string-method reference.
Check whether file reading already changed the line endings
If the string came from a text file, the characters you see after reading may not exactly match the file’s original bytes. Python text input can translate recognized newline conventions to n; the behavior depends on how the file was opened and its text I/O configuration. As a result, a later cleanup step may not find a r even if the file used CRLF line endings.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsWhen preserving or diagnosing original line endings matters, inspect the newline setting used by open() before applying a cleanup. The historical explanation of universal-newline input is in PEP 278.
Do not confuse newline removal with trimming
strip() and rstrip() are for removing whitespace at the beginning or end of a string, not for deleting newline characters throughout it. They also remove whitespace beyond just CR and LF. Use them when trimming is the goal; use replace() or splitlines() when the goal is to remove internal line boundaries.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




