Removing Spaces In Python 1

Removing Spaces in Python

Python is a high-level, general-purpose programming language with emphasis on code readability. It supports various programming paradigms, for example, structured, object-oriented, and functional programming. Oftentimes, Python is described as “batteries included”, thanks to its comprehensive standard library.

In this guide, we are going to learn various ways of removing whitespaces from a string in Python.

Prerequisites

To perform the steps demonstrated in this guide, you will need the following components:

Step 1 – Creating a Python Script

For demonstration purposes, we are going to create a Python script remove-space.py and put all our codes there. Then, we are going to run the script with the help of the Python interpreter.

First, create the Python script:

create a file

Open the script in a text editor:

nano editor

Next, we are going to create a string variable s and assign a string:

create string variable

If we include the print() function, we can see how the string is interpreted:

include print to the file

Here,

    • The print() function is taking a string as an argument.

    • The print() function can interpret backslash characters.

Run the script:

run script

Step 2 – Removal of Lead/Tail Spaces

With the help of the strip() function, we can remove the leading and tailing characters of a string.

The following code demonstrates using the strip() function on the variable s:

Removal of Lead/Tail Spaces

run Removal of Lead/Tail Spaces script

Note that the strip() function removes all the leading and tailing spaces. If you want to remove just the leading or tailing spaces, then use lstrip() or rstrip() respectively:

different ways of using strip

running different ways of using strip script

Step 3 – Removal of All Whitespaces

With the help of the replace() function, we can replace contents in a string. Taking advantage of this feature, we can replace all the whitespaces with nothing, thus removing them.

Let’s put the replace() function in action. Check out the following code:

removal of all whitespaces

run removal of whitespaces script

Here,

    • The first parameter of replace() describes what pattern to look for in the given string.

    • The second parameter of replace() describes what will be the replacement content.

Step 4 – Removal of Whitespaces using split() and join()

In this section, we are going to use the split() and join() functions.

    • split(): It takes a string and breaks it up into a list. The breaking points are determined by a delimiter.

    • join(): It takes the list and puts it back into a single string. The parts are joined using a single whitespace (” “).

Let’s put these functions in action. Have a look at the following code:

use split and join

run script of split and join

Here,

    • We have combined the usage of split(), join(), and print() in a single line.

    • The output of the split() function is passed as an argument for the join() function.

    • The output of the join() function is passed as an argument for the print() function.

Step 5 – Removal of Whitespaces using translate()

In Python, the translate() function replaces specified characters with characters defined in a dictionary or a mapping table.

In this example, we are going to use the string.whitespace dictionary that contains all the whitespace characters.

Have a look at the following code:

remove whitespace using translate

remove whitespace using translate script

Step 6 – Removal of Whitespace using Regex

Regular expression (or “regex” for short) is a powerful feature in many programming languages. Any regular expression will consist of a series of characters that construct a search pattern. A regular expression can be used to check if a string contains the specified pattern.

Python also supports regex, significantly improving its text manipulation capabilities. In this section, we will use regex to eliminate any whitespace character found on our test string.

Take a look at the following code:

use regex to remove whitespaces

run the script to remove whitespaces by using regex

Here,

    • We are importing re, a dedicated package for working with regular expressions.

Final Thoughts

In this guide, we demonstrated various ways of dealing with whitespaces in a string with Python. In the process, we also learned simple usage of various functions like split(), join(), replace(), translate(), etc.

Interested in learning more about Python? Check out the following guides: