Python | Remove spaces from a string
Given a string, write a Python program to remove all spaces from it.
Examples:
Input : g e e k Output : geek Input : Hello World Output : HelloWorld
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 Python.
Method 1: Remove spaces from a string using replace() function
Python3
# Python3 code to remove whitespacedef remove(string): return string.replace(" ", "") # Driver Programstring = ' g e e k 'print(remove(string)) |
Output
geek
Method 2: Remove spaces from a string using split() and join()
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 whitespacedef remove(string): return "".join(string.split()) # Driver Programstring = ' g e e k 'print(remove(string)) |
Output
geek
Method 3: Remove spaces from a string using Python regex
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.
Python3
# Python3 code to remove whitespaceimport redef remove(string): pattern = re.compile(r'\s+') return re.sub(pattern, '', string) # Driver Programstring = ' g e e k 'print(remove(string)) |
Output
geek
Method 4: Remove spaces from a string using translate()
translate() returns a string that is a modified string of givens string according to given translation mappings.
Python3
# Python code to remove whitespaceimport stringdef remove(string): return string.translate(None, ' \n\t\r') # Driver Programstring = ' g e e k 'print(remove(string)) |
Output
geek
Method 5: Remove spaces from a string using reduce function and conditional statement
Reduce function iterate over the string and it joins to the element of string to result if it is not white space.
Python3
# Python code to remove whitespace#importing modulefrom functools import reduce#Function to remove white spacedef remove(string): return reduce(lambda x, y: (x+y) if (y != " ") else x, string, ""); # Driver Programstring = ' g e e k 'print(remove(string)) |
Output:
geek
Method 6: Remove spaces from a string using lstrip() function
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.
Method 7: Remove spaces from a string using rstrip() function
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
Method 8 : Using isspace() method
Python3
# Python3 code to remove whitespacedef remove(string): ns="" for i in string: if(not i.isspace()): ns+=i return ns # Driver Programstring = ' g e e k 'print(remove(string)) |
geek


