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

“Learn everyday to grow everyday”

In this tutorial we will study about rstrip, lstrip, strip, replace and regular expression to

  • Remove (strip) space at the start of the string in Python
  • Remove (strip) space at the end of the string in Python
  • Remove (strip) white spaces from start and end of the string.
  • Remove all the spaces in python
  • Remove Duplicate Spaces in Python

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

Output:

“This 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’

 

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 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:

‘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 ‘

 

 

 

Leave a comment