This tutorial will help you learn how to remove \r\n from a string or list of strings in Python in some of the simplest ways. Continue reading the end of this tutorial.
Remove \r\n from a string or list of strings in Python
So, here are 3 simple ways to introduce to you in this article:
Using strip() method
Strip()
method will remove leading and trailing characters. So, we can use the strip()
method to remove \r\n
from a string or list of strings. Follow the syntax and code example below:
Syntax:
str.strip(char)
Parameters:
- char: the characters that you want to remove
Return value: a new string that removed leading and trailing.
Code example:
str = "\r Learn Share IT \r\n" # Remove \r\n from a string of strings res = str.strip() print(res) # Initial a list contain strings arr = ['\r line 1\r\n', '\n line 2\r\n', '\r\n line 3\r\n'] # Remove \r\n from a list of strings res1 = [i.strip() for i in arr ] print(res1)
Output:
Learn Share IT
['line 1', 'line 2', 'line 3']
Using replace() method
We can remove \r\n
from a string or list by using replace
method() to replace \r\n
with an empty string (“”). So, let’s check out the code.
Syntax:
str.replace(old_string,new_string)
Parameters:
- old_string: The string which you want to search.
- new_string: The string which you want to replace.
Return value: a string that is removed.
Code example:
str = "\r Learn Share IT \r\n" # Remove \r\n from a string of strings res = str.replace("\n","").replace("\r","") print(res) # Initial a list contain strings arr = ['\r line 1\r\n', '\n line 2\r\n', '\r\n line 3\r\n'] # Remove \r\n from a list of strings res1 = [i.replace("\n","").replace("\r","") for i in arr ] print(res1)
Output:
Learn Share IT
['line 1', 'line 2', 'line 3']
Using the re.sub() function
Similar to the replace()
method, we will use re.sub()
to replace all \n with an empty string (“”). But to use this function, first, you need to import re
.
See below to understand.
Syntax:
re.sub(value, repl, str)
Parameters:
- value: The string which you want to make replace.
- repl: The string will replace.
- str: The original string.
Code example:
import re str = "Learn \n Share IT \n\n" # Remove \r\n from a string of strings res = re.sub("\n","",str) print(res) # Initial a list contain strings arr = ['\n line \n 1\n\n', '\n line \n2\n\n', '\n\n l\nine 3\n\n'] # Remove \r\n from a list of strings res1 = [re.sub("\n","",i) for i in arr ] print(res1)
Output:
Learn Share IT
['line 1', 'line 2', 'line 3']
Summary
Finally, we have learned 3 ways to remove \r\n from a string or list of strings in Python. So, if you have any questions, please comment below. Thanks for reading, and see you again!
Maybe you are interested:
- Remove an element from a list by index in python
- Convert a list of characters into a string in Python
- Split the elements of a list in Python

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript