How to Remove Newline From Python String(7 Easy Methods)

remove newline from a string in python

Newline characters (\n) often appear when working with text in Python. They can create unwanted line breaks in output.

To remove newlines, Python provides methods like replace(), strip(), splitlines(), and re.sub(). The best method depends on where the newline appears in the string.

In this guide, you will learn simple ways to remove newline characters in Python with easy examples.

How To Remove a New Line From a String in Python?

Let’s discuss all the methods in detail, with example codes and analogies.

1st Method: Remove Newline Using replace()

The replace() method removes all newline characters (\n) from a string. It works anywhere in the string.
To use it, call replace() on the string. The first parameter is the character to remove, and the second parameter is an empty string (""), which removes the newline completely.
Incorrect line breaks may sometimes cause syntax errors, such as the EOL While Scanning String Literal error in Python.

Syntax

string.replace(old, new)
  • old: substring to replace
  • new: replacement string

Code

multi_line_string="\n\n\n How to \n remove \n new line \n\n from string \n in Python \n\n\n"
cleaned_string=multi_line_string.replace("\n","")
print(cleaned_string)

Output

 How to  remove  new line  from string  in Python

Handling Windows Line Endings

Sometimes text contains both \r and \n. In this case, you can follow the code below.

text = "Hello\r\nWorld\r\n"

cleaned = text.replace("\r", "").replace("\n", "")

print(cleaned)

Output

HelloWorld

2nd Method: Remove Newline From the End Using rstrip()

The rstrip() method removes whitespace and newline characters from the end of a string.

Syntax

string.rstrip()

Code

multi_line_string="\n\n\n How to \n remove \n new line \n\n from string \n in Python \n\n\n"
cleaned_string=multi_line_string.rstrip()
print(cleaned_string)

Output

 How to 
 remove 
 new line 

 from string
 in Python

3rd Method: Remove Newline From the Start Using lstrip()

The lstrip() method stands for left-strip that removes the leading spaces i-e spaces and new lines from the left (start) of the string while rstrip() strips newline from the end of the string.

Syntax

string.lstrip()

Code

multi_line_string="\n\n\n How to \n remove \n new line \n\n from string \n in Python \n\n\n"
cleaned_string=multi_line_string.lstrip()
print(cleaned_string)

Output

How to 
 remove
 new line

 from string
 in Python


4th Method: Remove Newlines From Start And End Using strip()

The strip() method strips newlines and spaces from both ends of the string. It is equivalent to using both lstrip() and rstrip() together.

Syntax

string.strip()

Code

multi_line_string="\n\n\n How to \n remove \n new line \n\n from string \n in Python \n\n\n"
cleaned_string=multi_line_string.strip()
print(cleaned_string)

Output

How to 
 remove
 new line

 from string
 in Python

This method does not remove newline characters from the middle of the string. However, if you want to remove spaces inside a string as well, you can follow our guide on how to remove spaces from a string in Python.

Real-World Example

Imagine your car gets dirty after a long trip. If you clean only the front, it is like using lstrip(). If you clean only the back, it is like using rstrip(). However, if you clean both the front and back while leaving the middle unchanged, it is like using strip().

lstrip(), rstrip(), and strip() comparison
Cleaning a Car

5th Method: Remove Newlines Using split() and join()

Imagine a chain made of steel and copper links. You want to remove the copper links. So, you break the chain, remove the copper parts, and join the steel parts again. As a result, you get a chain made only of steel, just as you wanted.

split() and join() method
Separating different links of a chain

In Python, the split() and join() methods work together to process strings.
First, split() breaks a string at a specified character, such as newline characters (\n), and returns a list of substrings.
Then, join() combines the list elements into a single string. Using an empty string ("") as the separator removes the newline characters and keeps the text continuous.

Syntax

string.split(separator)
separator.join(iterable)
  • separator (str): The character or substring where the string will be split.
  • iterable (list/tuple): A collection of strings that will be joined together.

Code

multi_line_string="\n\n\n How to \n remove \n new line \n\n from string \n in Python \n\n\n"
# split string at newline characters
strings_list=multi_line_string.split("\n")
# join parts without separator
cleaned_string="".join(strings_list)
print(cleaned_string)

Output

 How to  remove  new line  from string  in Python

This method is easy to read and works well for basic text processing.

6th Method: Remove Newlines Using splitlines()

The splitlines() method splits a string based on line breaks such as:

  • \n
  • \r
  • \r\n

It works like split("\n"), but it automatically supports multiple newline types. After splitting, you can use the join() method to combine the list elements back into one string. This method is useful for cleaning text. Additionally, if you want to shorten text or extract part of a string, you can learn how to truncate a string in Python.

Syntax

string.splitlines(keepends=False)
separator.join(iterable)
  • keepends (bool, optional): If True, line break characters (\n, \r, \r\n) are included in the resulting list. Default is False.
  • separator (str): A string to insert between elements when joining the list back into a single string.

Code

multi_line_string="\n\n\n How to \n remove \n new line \n\n from string \n in Python \n\n\n"
strings_list=multi_line_string.splitlines()
cleaned_string="".join(strings_list)
print(cleaned_string)

Output

 How to  remove  new line  from string  in Python

This method is often used when processing text files.

7th Method: Remove Newlines Using Regular Expressions

Regular expressions are powerful for handling complex text patterns and removing unwanted characters. To remove newlines, we use the re module and its sub() method. First, import the module using import re. Then use re.sub(r'\n', '', string) to remove newline characters. Here, r represents a raw string that treats backslashes as normal characters. As a result, all newline characters are removed from the string.

Syntax

import re

re.sub(pattern, replacement, string)
  • pattern: The text pattern you want to find (in this case, newline characters like \n).
  • replacement: The value that will replace the pattern (use '' to remove it).
  • string: The original string you want to modify.

Code

import re 
multi_line_string="\n\n\n How to \n remove \n new line \n\n from string \n in Python \n\n\n"
cleaned_string=re.sub(r'\n','',multi_line_string)
print(cleaned_string)

Output

How to  remove  new line  from string  in Python

This approach is useful when you want to replace multiple line breaks with a single space.

Comparison Table for All Methods

MethodRemoves StartRemoves MiddleRemoves EndSplits StringRejoins StringBest Use Case
replace()YesYesYesNoNoRemove all newlines quickly
rstrip()NoNoYesNoNo
Remove trailing newline
lstrip()YesNoNoNoNoRemove leading newline
strip()YesNoYesNoNoClean text edges
split() + join()YesYesYesYesYesClean readable approach
splitlines() + join()YesYesYesYesYesHandling multiple line types
sub() (from re)YesYesYesNoNoComplex newline patterns

Why Remove New Lines from a String?

Removing newline characters is common when working with text data. Typical situations include:

  • Data cleaning: Extra line breaks can make text difficult to process.
  • Formatting output: Removing unnecessary newlines improves readability.
  • User input processing: Users sometimes submit text with accidental line breaks.
  • File processing: When reading files, newline characters often appear automatically.

Try It Yourself

You can run these examples instantly using the Syntax Scenarios Python Compiler. It allows you to test Python code directly in your browser without installing Python.

This replaces multiple newline characters with a single space.

Conclusion

Removing newline characters in Python is an important part of text cleaning and formatting. Python provides several easy methods such as replace(), strip(), splitlines(), and regular expressions.

For most cases, replace() and splitlines() are the easiest and fastest solutions. However, the best method depends on where the newline appears in the string.

FAQs

How do you remove newline characters in Python?

You can remove newline characters using replace(), strip(), splitlines(), or re.sub(). The best method depends on whether you want to remove newlines from the beginning, end, or entire string.

Does strip() remove newline characters?

Yes. The strip() method removes whitespace characters including newline (\n) from the beginning and end of a string. However, it does not remove newline characters inside the string.

What is the fastest way to remove newlines in Python?

replace("\n","") is usually the fastest and simplest method.

How do I replace multiple newlines with one space?

You can use regular expressions:

import re
re.sub(r'[\r\n]+', ' ', text)

Scroll to Top