Removing spaces from a string involves eliminating any whitespace characters within the string. This can be accomplished using various methods in Python. By removing spaces from a string, you can manipulate and process the string more easily, perform comparisons, or use it in various other operations. In this article, we will see how to remove whitespace from string Python.
Input: g e e k
Output: geek
Input: Hello World
Output: HelloWorld
Explanation: In This, we are removing whitespaces from a string Python.
Remove Spaces from a String in Python
There are various approaches to removing whitespaces in a string. The first one is the Naive approach, which has been discussed in this article. But here we will discuss all the approaches which are specific to removing whitespace from string Python.
- Using replace()
- Using split() and join()
- Using translate()
- Using Reduce function
- Using lstrip() function
- Using rstrip() function
- Using isspace() method
- Using Regex and itertools
- Using regex.findall() method
- Using map() and lambda() function
- Using for loop and join()
- Using NumPy
Remove Whitespace from String using Replace()
In this example, we will remove whitespace from the string Python using replace(). We have taken a string and we have replaced all whitespaces with empty string.
Python3
# Python3 code to remove whitespace def remove(string): return string.replace( " " , "") # Driver Program string = ' g e e k ' print (remove(string)) |
Output
geek
Remove Blank Spaces from String using Split() and Join()
We will remove whitespace from string Python using split() and Join() in this example. First, we use the split() function to return a list of the words in the string, using sep as the delimiter Python string. Then, we use join() to concatenate the iterable.
Python3
# Python3 code to remove whitespace def remove(string): return "".join(string.split()) # Driver Program string = ' g e e k ' print (remove(string)) |
Output
geek
Remove Whitespace from String using Translate()
In this example, we will remove whitespace from the string Python using translate(). translate() returns a string that is a modified string of givens string according to given translation mappings.
Python3
# Python code to remove whitespace import string def remove(string): return string.translate( None , ' \n\t\r' ) # Driver Program string = ' g e e k ' print (remove(string)) |
Output
geek
Delete Blank Spaces from String using Reduce() function
In this example, we will remove whitespace from the string Python using reduce(). Reduce function iterate over the string and it joins to the element of the string to result if it is not white space.
Python3
# Python code to remove whitespace #importing module from functools import reduce #Function to remove white space def remove(string): return reduce ( lambda x, y: (x + y) if (y ! = " " ) else x, string, ""); # Driver Program string = ' g e e k ' print (remove(string)) |
Output
geek
Remove Whitespace from String using lstrip() function
We will remove whitespace from the string Python using lstrip() in this example. The lstrip() creates a new string by either deleting whitespace from the string’s “left” side or its leading whitespace.
Python3
string = "www.geeksforgeeks.org www." string_new = string.lstrip( "w." ) print (string_new) |
Output
geeksforgeeks.org www.
Delete Whitespace from String using rstrip() function
We will remove whitespace from the string Python using rstrip() in this example. The rstrip() creates a new string by removing the trailing whitespace. Eliminating the white spaces from the string’s “right” side makes it simpler to recall.
Python3
string = "www.geeksforgeeks.org www." string_new = string.rstrip( "w." ) print (string_new) |
Output
www.geeksforgeeks.org
Trim Extra Spaces from String using isspace() method
In this example, we will remove whitespace from the string Python using isspace(). we have taken a string and with a for loop we are traversing through the string and if the space is occurring then we are skipping the concatenation of the string character else we are assigning it.
Python3
# Python3 code to remove whitespace def remove(string): ns = "" for i in string: if ( not i.isspace()): ns + = i return ns # Driver Program string = ' g e e k ' print (remove(string)) |
Output
geek
Remove Whitespace from String using Regex and itertools
In this example, we will remove whitespace from the string Python using Regex and itertools.filterfalse(). In order to find a string or group of strings, a Regular Expression (RegEx) is a unique string of characters. It may compare a text to a pattern to determine whether it is present or absent. It can also divide a pattern into one or more sub-patterns. The function filters out all whitespace characters from the input string using the itertools.filterfalse() method, producing an iterable that only contains non-whitespace characters. To create the final output string, the process first turns iterable into a list and uses join() to concatenate all of its members.
Python3
# Python code to remove whitespace import re import itertools def remove(string): pattern = re. compile (r '\s+' ) string = re.sub(pattern, '', string) resList = list (itertools.filterfalse( lambda x: x = = ' ' , string)) return ''.join(resList) string = " g e e k " print (remove(string)) |
Output
geek
Trim Extra Spaces from String using regex.findall() method
The remove() method in this Python code uses regular expressions to eliminate any whitespace characters from a specified text. The function first locates all of the alphabetic non-whitespace characters in the input string using the re. find () method, and then it concatenates them using the. join() method. The remove function is called by the driver program, which first constructs a string variable called string containing whitespace characters before sending the string as input.
Python3
# Python3 code to remove whitespace import re def remove(string): return ' '.join(re.findall(r' [a - zA - Z] + ', string)) # Driver Program string = ' g e e k ' print (remove(string)) |
Output
geek
Remove Whitespace from String using Map() and Lambda() function
In this example, we have defined the string variable with whitespace, we have split it into a list of substrings, remove leading and trailing whitespaces from each substring using map(), lambda function, convert the map() object to a list, join the list of substrings into a single string using ”.join(), assign the resulting string to “string” variable, and print the final “string” variable without whitespaces.
Python3
string = ' g e e k ' string = ''.join( list ( map ( lambda x: x.strip(), string.split()))) print (string) |
Output
geek
Remove Whitespace from String using For Loop and Join()
In this example, we have taken a string and with a for loop we are traversing through the string and if the space is occurring then we are skipping the concatenation of the string character else we are assigning it.
Python3
string = "Hello World" new_string = "" for char in string: if char ! = " " : new_string + = char string = "".join(new_string) print (string) # Output: HelloWorld |
Output
HelloWorld
Trim Blank Spaces from String using NumPy
In this example, we will remove whitespace from the string Python using Numpy. we have imported NumPy and we have created a remove() and replaced all occurrences of whitespaces in the input string with an empty string and returned a final string.
Python3
import numpy as np def remove(string): return np.char.replace(string, ' ' , '') # Driver Code string = ' g e e k ' print (remove(string)) |
Output
geek