Signup/Sign In

How to Remove Extra Whitespaces from a String in Python?

Posted in Programming   LAST UPDATED: AUGUST 25, 2021

    There are many instances where the removal of empty spaces or white spaces in a string becomes a necessity. This could be required when working on the operation of strings or as a part of a bigger application that needs some pre-processing. Whatever be the requirement, the process of eliminating whitespaces can be done in many ways, we are sure you can come up with an intuitive logic too. We will look at a few of them today.

    1. Using the replace method

    The replace method is used to replace a given string/character with some other string/character or nothing. We can use it to replace all the whitespaces with a specific character or just remove all the spaces.

    Time for an example:

    my_string = ' Stud y toni g h    t'
    print(my_string.replace(" ", "")) 
    

    Output:

    Studytonight
    


    2. Using the split method and join method

    The split method can be combined with the join method to remove all the whitespaces present in a string.

    Time for an example:

    my_string = ' Stud y toni g h    t'
    print("".join(my_string.split())) 
    

    Output:

    Studytonight
    


    3. Using regular expressions

    Regular expressions are used to define a pattern that can be applied to a string. This is widely used, but not suited when the pre-processing is generic, or when it needs to be suited for a specific usage only. So, we will be using a regular expression to remove white spaces from any given string.

    Time for an example:

    import re 
    
    my_string = ' Stud y toni g h    t'
    my_pattern = re.compile(r'\s+')
    print(re.sub(my_pattern, '', my_string)) 

    Output:

    Studytonight


    4. Using the translate method

    The translate method returns a string wherein every character in the string is matched to a specific character or value which is based on a translation table.

    Time for an example:

    import string 
    
    my_string = ' Stud y toni g h    t'
    print(my_string.translate({ord(c): None for c in string.whitespace}))

    Output:

    Studytonight


    5. Using the strip method

    The strip method can be used to eliminate the leading and trailing white spaces in a string. This will not eliminate the whitespaces present between characters within a string.

    Time for an example:

    my_string = ' Stud y toni g h    t '
    my_string.strip()

    Output:

     'Stud y toni g h    t'

    Note: The lstrip and rstrip methods can be used to strip the trailing spaces from the left and the right side respectively. Below is a demonstration of both the functions:

    my_string = "   Stud y toni g h    t   "
    print("Lstrip in action")
    print(my_string.lstrip())

    Output:

    Stud y toni g h    t   

    Note: The 't' in the output is the last letter and it is followed by 3 trailing spaces.

    my_string = "   Stud y toni g h    t   "
    print("Rstrip in action")
    print(my_string.rstrip())

    Output:

       Stud y toni g h    t

    Note: The output begins with 3 spaces


    Conclusion:

    In this post, we understood a couple of ways to remove empty spaces which are present in the string. Don't forget to let us know which method you would use in the comment section below.

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:PythonPython String
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS