Warning: session_start(): open(/tmp/sess_3165ea7f333f0720b8f66271dfab92ee, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Remove \r\n From A String Or List Of Strings In Python - LearnShareIT

How To Remove \r\n From A String Or List Of Strings In Python

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

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:

Leave a Reply

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