Remove \r\n from a string (or list of strings) in Python

avatar

Borislav Hadzhiev

Last updated: Jul 8, 2022

banner

Photo from Unsplash

Remove \r\n from a string (or list of strings) in Python #

Use the str.rstrip() method to remove \r\n from a string in Python, e.g. result = my_str.rstrip(). The str.rstrip() method returns a copy of the string with the trailing whitespace removed.

main.py
my_str = "Hello world\r\n" # ✅ remove \r\n from string result = my_str.rstrip() print(repr(result)) # 👉️ 'Hello world' # ------------------------------------- # ✅ remove \r\n from all strings in list with open('example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) # 👉️ ['First line\r\n', 'Second line\r\n', 'Third line\r\n'] result = [line.rstrip() for line in lines] print(result) # 👉️ ['First line', 'Second line', 'Third line']

The \r\n character is used as a newline character in Windows.

The str.rstrip method returns a copy of the string with the trailing whitespace removed.

main.py
my_str = "Hello world\r\n" # ✅ remove \r\n from string result = my_str.rstrip() print(repr(result)) # 👉️ 'Hello world'

There is also a str.strip method, which returns a copy of the string with the leading and trailing whitespace removed.

main.py
my_str = "\r\nHello world\r\n" result = my_str.strip() print(repr(result)) # 👉️ 'Hello world'

You can also use the re.sub method to remove all \r\n characters in the string.

main.py
import re my_str = "\r\nHello world\r\n" result = re.sub(r'\r\n', '', my_str) print(repr(result)) # 👉️ 'Hello world'

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

The first argument we passed to the method is a regular expression that matches \r\n characters, and the second is the replacement string.

To replace \r\n from a list of strings:

  1. Use a list comprehension to iterate over the list.
  2. Call the str.rstrip() method to remove trailing \r\n characters.
  3. The strings in the new list won't contain any \r\n characters.
main.py
with open('example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) # 👉️ ['First line\r\n', 'Second line\r\n', 'Third line\r\n'] result = [line.rstrip() for line in lines] print(result) # 👉️ ['First line', 'Second line', 'Third line']

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we call the str.rstrip() method on the string to remove any trailing whitespace.

You could also call the str.strip() method to remove all leading and trailing whitespace or the re.sub() method to remove all \r\n characters from the string.

main.py
import re # ✅ remove \r\n from all strings in list with open('example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) # 👉️ ['First line\r\n', 'Second line\r\n', 'Third line\r\n'] result = [re.sub(r'\r\n', '', line) for line in lines] print(result) # 👉️ ['First line', 'Second line', 'Third line']

The example above assumes that there is a file named example.txt located in the same directory as the Python script.

The file has the following contents:

example.txt
First line Second line Third line
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.