Last updated: Apr 9, 2024
Reading timeยท5 min
To remove the newline characters from a list:
str.strip()
method to remove the leading and trailing newlines from
each string.my_list = ['bobby\n', 'hadz\r\n', '\ncom\r\n'] # โ Remove leading and trailing newline characters from list new_list = [item.strip() for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com'] # --------------------------------------------- # โ Remove ALL newline characters from list new_list = [ item.replace('\r', '').replace('\n', '') for item in my_list ] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
If you need to remove the newline characters from a string, use the following code sample instead.
my_str = '\r\nfirst row\r\n' print(repr(my_str)) # ๐๏ธ 'first row\r\n' # โ remove leading and trailing carriage returns result_1 = my_str.strip() print(repr(result_1)) # ๐๏ธ 'first row' # --------------------------------------------- # โ remove only trailing carriage returns result_2 = my_str.rstrip() print(repr(result_2)) # ๐๏ธ '\r\nfirst row' # --------------------------------------------- # โ remove all carriage returns from string result_3 = my_str.replace('\r', '').replace('\n', '') print(repr(result_3)) # ๐๏ธ 'first row'
The examples use a list comprehension to iterate over the list.
On each iteration, we use the str.strip()
method to remove the leading and
trailing newlines from each string.
my_list = ['bobby\n', 'hadz\r\n', '\ncom\r\n'] new_list = [item.strip() for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The str.strip() method returns a copy of the string with the leading and trailing whitespace removed.
The strings in the new list won't contain any leading and trailing newline characters.
The list comprehension doesn't mutate the original list, it returns a new list.
If you need to mutate the original list, use a slice.
my_list = ['bobby\n', 'hadz\r\n', '\ncom\r\n'] my_list[:] = [item.strip() for item in my_list] print(my_list) # ๐๏ธ ['bobby', 'hadz', 'com']
We used the my_list[:]
syntax to get a slice that represents the entire list,
so we can assign to the variable directly.
my_list[:]
represents the entire list, so when we use it on the left-hand side, we are assigning to the entire list.This approach changes the contents of the original list.
If you only need to remove the leading or trailing newline characters from each
string in a list, use the str.lstrip()
or
str.rstrip() methods.
my_list = ['bobby\n', 'hadz\r\n', '\ncom\r\n'] new_list = [item.rstrip() for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', '\ncom'] new_list = [item.lstrip() for item in my_list] print(new_list) # ๐๏ธ ['bobby\n', 'hadz\r\n', 'com\r\n']
The str.rstrip() method returns a copy of the string with the trailing whitespace removed.
The str.lstrip() method returns a copy of the string with the leading whitespace removed.
Alternatively, you can use the str.replace()
method.
This is a three-step process:
str.replace()
method to remove all newline characters from each
string.my_list = ['bobby\n', 'hadz\r\n', '\ncom\r\n'] new_list = [item.replace('\r', '').replace('\n', '') for item in my_list] print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
We used the str.replace()
method to remove the newline characters from each
string in the list.
The str.replace() method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
We provided an empty string as the replacement because we want to remove all newline characters.
Notice that we called the str.replace()
method 2 times.
This is necessary because the newline characters differ between operating systems.
Windows uses \r\n
as an end-of-line character, whereas \n
is the default in
Unix.
for
loopThis is a three-step process:
for
loop to iterate over the list.str.strip()
method on each list element.list.append()
method to append the result to a new list.a_list = [' bobby\n ', ' hadz\n ', ' com\n '] new_list = [] for item in a_list: new_list.append(item.strip()) print(new_list) # ๐๏ธ ['bobby', 'hadz', 'com']
We used a for loop to iterate over the list.
str.strip()
method on the current list item and append the result to a new list.The list.append() method adds an item to the end of the list.
If you need to mutate the original list instead of creating a new list, use the
enumerate()
function.
a_list = [' bobby\n ', ' hadz\n ', ' com\n '] for index, item in enumerate(a_list): a_list[index] = item.strip() print(a_list) # ๐๏ธ ['bobby', 'hadz', 'com']
The enumerate() function takes an iterable and returns an enumerate object containing tuples where the first element is the index and the second is the corresponding item.
my_list = ['bobby', 'hadz', 'com'] for index, item in enumerate(my_list): print(index, item) # ๐๏ธ 0 bobby, 1 hadz, 2 com
On each iteration, we reassign the item at the current index to the result of
calling the str.strip()
method on it.
Which approach you pick is a matter of personal preference. I'd use a list comprehension because I find them quite direct and easy to read.
You can learn more about the related topics by checking out the following tutorials: