Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Python Remove Spaces from String

Python provides various ways to remove white-spaces from a String. This article will focus of some of the efficient techniques to remove spaces from a String.

Either of the following techniques can be used to get rid of the spaces from a string:

  • By using strip() method
  • By using replace() method
  • By using join() with split() method
  • By using translate() method
  • By using Regular Expressions

Method 1: By using strip() method

The split() function basically removes the leading and the trailing spaces of the string.

Key points:

  • strip(): This function removes the leading and the trailing spaces from an array including the tabs(\t).
  • lstrip(): This function removes spaces from the left end of the string.
  • rstrip(): This function removes spaces from the right end of the string.


string = '   Safa Mulani is a student of Engineering Discipline.    '

print(f'String =\'{string}\'')

print(f'After Removing Leading Whitespaces String =\'{string.lstrip()}\'')

print(f'After Removing Trailing Whitespaces String =\'{string.rstrip()}\'')

print(f'After Trimming Whitespaces String =\'{string.strip()}\'')


Output Strip Function
Output-strip() Function

Method 2: By using replace() method

This function removes all the spaces from the string, removes spaces between words too.


def remove(string_input): 
    return string_input.replace(" ", "") 

string_input = ' S a f a '



Method 3: By using join() and split() method

The join() and split() function work in accordance altogether. Firstly, the split() method returns a list of words in the entire string using a delimiter. Then we need to use the join() method to concatenate them.


def remove(string_input): 
    return "".join(string_input.split()) 
# Driver Program 
string_input= ' S a f a '



Method 4: By using translate() method

Translate Function
translate() Function

In the above example, the translate() function removes all of the white-spaces from the string and results in a compact string as output.

Method 5: By using Regular Expressions

The re.sub() function is used to remove the white-spaces from a string.

import re

string = '  Engineering and Medical are altogether different disciplines. \t\n Agreed!!  '

print('String after removal of all the spaces using Regular Expressions:\n', re.sub(r"\s+", "", string), sep='') 

print('String after removal of leading spaces using Regular Expressions:\n', re.sub(r"^\s+", "", string), sep='')  # ^ matches start

print('String after removal of trailing spaces using Regular Expressions:\n', re.sub(r"\s+$", "", string), sep='')  # $ matches end

print('String after removal of leading and trailing spaces using Regular Expressions:\n', re.sub(r"^\s+|\s+$", "", string), sep='')  # | for OR condition


Output Removal Of Spaces Using RegEx
Output-Removal Of Spaces Using RegEx


Thus, in this article we have implemented various techniques to remove white-spaces from an input string in Python.


Leave a Reply

Your email address will not be published. Required fields are marked *