October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Remove Newlines From a String in Python (and the r Trap)

Use replace() to remove specific newline characters from a Python string, and learn why deleting n alone may leave the r from CRLF input.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When 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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.