Python | Removing newline character from string

Many times, while working with Python strings, we can have a problem in which we have huge amount of data and we need to perform preprocessing of certain kind. This can also be removing stray newline characters in strings. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using loop
This task can be perform using brute force in which we check for “\n” as a string in a string and replace that from each string using loop.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Removing newline character from string
# using loop
  
# initialize list 
test_list = ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'geeks\n']
  
# printing original list 
print("The original list : " + str(test_list))
  
# Removing newline character from string
# using loop
res = []
for sub in test_list:
    res.append(sub.replace("\n", ""))
          
# printing result
print("List after newline character removal : " + str(res))

chevron_right


Output :

The original list : ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'geeks\n']
List after newline character removal : ['gfg', 'is', 'best', 'for', 'geeks']

 

Method #2 : Using regex
This task can also be executed using regex functions which can also perform the global replace of all the newline characters with empty string. Advantage over above method is that above method just removes one occurrences and this method checks every occurrence.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Removing newline character from string
# Using regex
import re
  
# initialize list 
test_list = ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'geeks\n']
  
# printing original list 
print("The original list : " + str(test_list))
  
# Removing newline character from string
# Using regex
res = []
for sub in test_list:
    res.append(re.sub('\n', '', sub))
          
# printing result
print("List after newline character removal : " + str(res))

chevron_right


Output :

The original list : ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'geeks\n']
List after newline character removal : ['gfg', 'is', 'best', 'for', 'geeks']



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.