Remove Space in Python – (strip Leading, Trailing, Duplicate spaces in string)

Remove space in python string / strip space in python string : In this Tutorial we will learn how to remove or strip leading , trailing and duplicate spaces in python  with lstrip() , rstrip() and strip() Function with an example for each . lstrip() and rstrip() function trims the left and right space respectively.  strip() function trims all the white space.

objective:

  • Remove (strip) space at the start of the string in Python – trim leading space
  • Remove (strip) space at the end of the string in Python – trim trailing space
  • Remove (strip) white spaces from start and end of the string – trim space.
  • Remove all the spaces in python
  • Remove Duplicate Spaces in Python
  • Trim space in python using regular expressions.

Let’s see the example on how to Remove space in python string / strip space in python string one by one.

 

Remove Space at the start of the string in Python (Strip leading space in python):

## Remove the Starting Spaces in Python

string1="    This is Test String to strip leading space"
print (string1)
print (string1.lstrip())

lstrip() function in the above example strips the leading space so the output will be

‘      This is Test String to strip leading space’

‘This is Test String to strip leading space’

 

 

Remove Space at the end of the string in Python (Strip trailing space in python):

## Remove the Trailing or End Spaces in Python

string2="This is Test String to strip trailing space     "
print (string2)
print (string2.rstrip())

rstrip() function in the above example strips the trailing space so the output will be

‘This is Test String to strip trailing space        ‘

‘This is Test String to strip trailing space’

 

 

Remove Space at the Start and end of the string in Python (Strip trailing and trailing space in python):

## Remove the whiteSpaces from Beginning and end of the string in Python

string3="    This is Test String to strip leading and trailing space      "
print (string3)
print (string3.strip())

strip() function in the above example strips, both leading and trailing space so the output will be

‘      This is Test String to strip leading and trailing space       ‘       

‘This is Test String to test leading and trailing space’

 

Remove or strip all the spaces in python:

## Remove all the spaces in python

string4="       This is Test String to test all the spaces   "
print (string4)
print (string4.replace(" ", ""))

The above example removes all the spaces in python. So the output will be

‘    This is Test String to test all the spaces  ‘

‘ThisisTestStringtotestallthespaces’

 

Remove or strip the duplicated space in python:

# Remove the duplicated space in python

import re
string4="   This is      Test String   to test   duplicate spaces   "
print (string4)
print (re.sub(' +', ' ',string4))
  • We will be using regular expression to remove the unnecessary duplicate spaces in python.
  • sub() function: re.sub() function takes the string4 argument and replaces one or more space with single space as shown above so the output will be.

‘    This  is      Test String     to test    duplicate spaces  ‘

‘ This is Test String to test duplicate spaces ‘

 

Using Regular Expression to trim spaces:

re.sub() function takes the string1 argument and apply regular expression to trim the white spaces as shown below


string1 = "  This is to test space     "

print('Remove all space:',re.sub(r"\s+", "", string1), sep='')  # trims all white spaces
print('Remove leading space:', re.sub(r"^\s+", "", string1), sep='')  # trims left space
print('Remove trailing spaces:', re.sub(r"\s+$", "", string1), sep='')  # trims right space
print('Remove leading and trailing spaces:', re.sub(r"^\s+|\s+$", "", string1), sep='') # trims both

so the resultant output will be


Remove all space:’Thisistotestspace’
Remove leading space:’This is to test space    ‘
Remove trailing spaces:’    This is to test space’
Remove leading and trailing spaces:’This is to test space’

 

previous small Remove Spaces in Python - (strip Leading, Trailing, Duplicate spaces in string) strip space in python                                                                                                         next small Remove space in Python - (strip Leading, Trailing, Duplicate spaces in string) strip space in python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.