How to remove space in python

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

def remove(string):

    return string.replace(" ", "")

string = ' 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

def remove(string):

    return "".join(string.split())

string = ' 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

import re

def remove(string):

    pattern = re.compile(r'\s+')

    return re.sub(pattern, '', string)

string = ' 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

import string

def remove(string):

    return string.translate(None, ' \n\t\r')

string = ' 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

from functools import reduce

def remove(string):

    return reduce(lambda x, y: (x+y) if (y != " ") else x, string, "");

string = ' 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

def remove(string):

    ns=""

    for i in string:

        if(not i.isspace()):

            ns+=i

    return ns   

string = ' g e e k '

print(remove(string))


How do I remove spaces from a string?

strip()—Remove Leading and Trailing Spaces. The str. strip() method removes the leading and trailing whitespace from a string.

How do I remove and replace spaces in Python?

The easiest approach to remove all spaces from a string is to use the Python string replace() method. The replace() method replaces the occurrences of the substring passed as first argument (in this case the space ” “) with the second argument (in this case an empty character “”).