Wednesday, December 25, 2024
Google search engine
HomeLanguagesRemoving newline character from string in Python

Removing newline character from string in Python

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

Remove Newline character from String in Python

Here, we will cover 4 different methods to Remove Newline From String in Python:

Use the replace function to Remove a Newline Character From the String in Python

This task can be performed using brute force in which we check for “\n” as a string in a string and replace that from each string using a loop. Here, The replace() in Python returns a copy of the string where all occurrences of a “\n” are replaced with an empty string.

Python3




# initialize list
test_list = ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'Lazyroar\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))


Output :

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

Use the strip() Function to Remove a Newline Character From the String in Python

The Python strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string. Our task can be performed using strip function() in which we check for “\n” as a string in a string. 

Python3




lis = "\n Geeks for  Geeks \n"
string = lis.strip()
print(string)


Output:

Geeks for  Geeks

Use the splitlines() method to Remove a Newline Character From the String in Python

Python splitlines() method is used to split the lines at line boundaries. The function returns a list of lines in the string, including the line break.

Python3




def func(value):
    return ''.join(value.splitlines())
 
 
mystring = "\n Geeks \n for \n  Geeks \n"
print("Original string:", mystring)
print("After deleting the new line:", func(mystring))


Output:

Actual string: 
 Geeks 
 for 
 Geeks 

After deleting the new line:  Geeks  for   Geeks 

Use the re.sub() Function to Remove a Newline Character From the String in Python

This task can also be executed using Python regex functions which can also perform the global replacement of all the newline characters with an empty string.

Python3




import re
 
# initialize list
test_list = ['gf\ng', 'i\ns', 'b\nest', 'fo\nr', 'Lazyroar\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))


Output:

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

Using a list comprehension and the join() method:

Approach:

Split the original string into a list of substrings using the split() method.
Join the list of substrings back into a string using the join() method, with an empty string (”) as the separator.
 

Python3




my_string = "hello\nworld\n"
new_string = ''.join(my_string.split('\n'))
print(new_string)


Output

helloworld

Time Complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments