Skip to content
Related Articles

Related Articles

Python | Remove spaces from a string
  • Last Updated : 17 Jan, 2019

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 remove 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.

Approach#1 : Using replace()

Using replace() function, we replace all whitespace with no space(“”).






# Python3 code to remove whitespace
def remove(string):
    return string.replace(" ", "")
      
# Driver Program
string = ' g e e k '
print(remove(string))
Output:
geek

 

Approach #2 : Using split() and join()

First we use split() function to return a list of the words in the string, using sep as the delimiter string. Then, we use join() to concatenate the iterable.




# Python3 code to remove whitespace
def remove(string):
    return "".join(string.split())
      
# Driver Program
string = ' g e e k '
print(remove(string))
Output:
geek

 
Approach #3 : Using Python regex




# Python3 code to remove whitespace
import re
  
def remove(string):
    pattern = re.compile(r'\s+')
    return re.sub(pattern, '', string)
      
# Driver Program
string = ' g e e k '
print(remove(string))
Output:
geek

 

Approach #4 : Using translate()




# 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :