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.
# 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)) |
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.
# 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)) |
The original list : ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'geeks\n'] List after newline character removal : ['gfg', 'is', 'best', 'for', 'geeks']
Recommended Posts:
- Python program for removing i-th character from a string
- How to print without newline in Python?
- Python | Removing unwanted characters from string
- Python | Removing Initial word from string
- Python | Ways to split strings using newline delimiter
- Python - Least Frequent Character in String
- How to capitalize first character of string in Python
- Python - Lowercase Kth Character in string
- Python | K Character Split String
- Python | Lowercase first character of String
- Python | Frequency of each character in String
- Python | Longest Run of given Character in String
- Python | Find position of a character in given string
- Python | Split string on Kth Occurrence of Character
- Python | Count occurrences of a character in string
- Python | Maximum frequency character in String
- Python - Group list by first character of string
- Python - Split String of list on K character
- Python - Rear character String categorization
- Python | First character occurrence from rear String
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.


