Strings are one of the fundamental data types in Python, storing a sequence of characters. With strings, you can perform many operations: splitting, joining, replacing, comparing, and more.
Sometimes, it's necessary to format strings by removing unnecessary characters, such as spaces.
This article will cover the methods available in Python (version 3.10.12) for removing spaces from strings.
Often, we need to remove only extra spaces, such as those at the start or end of a string. Python provides several similar methods for this purpose:
strip()
removes spaces from both the string's start and end.lstrip()
removes spaces only from the start.rstrip()
removes spaces only from the end.Here’s an example of how to clean up a string by removing spaces at the edges:
text_before = " This is a string with spaces "
text_after = text_before.strip()
text_after_left = text_before.lstrip()
text_after_right = text_before.rstrip()
print(text_after)
print(text_after_left)
print(text_after_right)
Console output:
This is a string with spaces
This is a string with spaces
This is a string with spaces
In some cases, you may need to remove all spaces entirely. This can be done by replacing every space character with an empty string:
text_before = " This is a string with spaces "
text_after = text_before.replace(' ', '')
print(text_after)
Console output:
Thisisastringwithspaces
Another way to remove all spaces is to use the translate()
method. While less intuitive, it can be more flexible in certain scenarios:
text_before = " This is a string with spaces "
text_after = text_before.translate({ord(' '): None})
print(text_after)
Console output:
Thisisastringwithspaces
The translate()
function takes a dictionary as an argument, where the keys are ASCII codes of characters to be replaced, and the values are the replacement characters. The ord()
function converts a character to its corresponding ASCII code.
With translate()
, you can replace multiple characters at once. For example:
text_before1 = " This is a string with spaces "
text_before2 = " 1 2 3 4 5 "
text_before3 = " { 'someData': 100, 'otherData': 'information' } "
space_table = str.maketrans({' ': None})
text_after1 = text_before1.translate(space_table)
text_after2 = text_before2.translate(space_table)
text_after3 = text_before3.translate(space_table)
print(text_after1)
print(text_after2)
print(text_after3)
Console output:
Thisisastringwithspaces
12345
{'someData':100,'otherData':'information'}
The simplest way to remove all repeated spaces in a string is to perform the following steps:
split()
function by spaces as delimiters, resulting in a list of substrings.join()
function with a single space as the separator.Here’s how this can be done:
text_before = " This is a string with spaces "
# Split the string into substrings; spaces are the default delimiter
text_splitted = text_before.split()
# Join the substrings into a single string using a space as the delimiter
text_after = ' '.join(text_splitted)
print(text_after)
In the console, you’ll see the formatted string without extra spaces:
This is a string with spaces
You can write the same operations more concisely:
text_before = " This is a string with spaces "
text_after = ' '.join(text_before.split())
print(text_after)
The console output will remain the same:
This is a string with spaces
Using this method, you can also replace spaces with any other character:
text_before = " This is a string with spaces "
text_after = '_'.join(text_before.split())
print(text_after)
In this case, the console output will be:
This_is_a_string_with_spaces
The methods shown earlier are effective for simple scenarios. However, strings often have more complex patterns, requiring advanced methods to remove spaces. A highly flexible way to handle string modifications is by using regular expressions.
Here’s an example:
import re # Import the module for working with regular expressions
# A string containing sequences of two or more spaces, as well as some single spaces
text_before = " This is a string with spaces . "
# Replace all sequences of two or more spaces with a single space
text_after = re.sub(r"\s+", " ", text_before)
print(text_after)
The console output will be a string where only single spaces remain:
This is a string with spaces .
This example introduces some problems:
Multiple spaces before the period at the end are replaced with a single space. However, there should not be any space before the period.
A sequence of spaces at the start of the string is replaced by a single space. However, there should not be any spaces at the beginning of the string.
We can resolve these issues by applying a sequence of transformations:
import re
text_before = " This is a string with spaces . "
# Remove spaces at the start and end of the string using the OR operator (|)
text_after = re.sub(r"^\s*|\s*$", "", text_before)
# Replace all repeated spaces with a single space
text_after = re.sub(r"\s+", " ", text_after)
# Replace all periods surrounded by spaces with just a period
text_after = re.sub(r"\s*[.]\s*", ".", text_after)
print(text_after)
The console output will now contain a properly formatted string without unnecessary spaces:
This is a string with spaces.
Here:
\s
: Matches any whitespace character (spaces, tabs, etc.).
+
: Matches one or more repetitions of the preceding element.
*
: Matches zero or more repetitions of the preceding element.
|
: Represents a logical OR
, allowing you to combine multiple conditions.
^
: Anchors the match at the beginning of the string.
$
: Anchors the match at the end of the string.
When using regular expressions, it’s important to understand the potential structure of the strings being processed to design an appropriate solution. For example:
Ultimately, removing spaces from a string in Python often requires a custom solution tailored to the specific case.
For more complex string manipulation (in this case, removing spaces), you can manually check each character in a loop with multiple conditions. This approach offers more flexibility and control over the process.
In the simplest case, removing spaces inside a loop looks like this:
# Define a function for more complex string processing logic
def complexRemoval(string):
after = ""
for i in string:
if not i.isspace(): # The isspace() function checks if the character is a space and returns a boolean result (True or False)
after += i
return after
text_before = " This is a string with spaces . "
text_after = complexRemoval(text_before)
print(text_after)
The console output will contain all the characters of the original string, but without spaces:
Thisisastringwithspaces.
Clearly, this isn't the desired result, so we need to complicate the logic for removal.
To refine the logic, we can introduce a variable to track whether the previous character was a space:
def complexRemoval(string):
after = ""
wasSpace = True # Variable to track if the previous character was a space
for i in string:
if not i.isspace(): # If the character is not a space
if i == '.' and wasSpace: # If we encounter a period and the previous character was a space, remove it
after = after[:len(after) - 1] # Remove the last character (space)
after += i
wasSpace = False
elif not wasSpace: # If it's a space but the previous character was not a space
after += i
wasSpace = True
return after
# Test cases
print(complexRemoval(" This is a string with spaces . "))
print(complexRemoval("Lots of different spaces blah blah blah . Also a period . "))
The output in the console will now show perfectly formatted strings without unnecessary spaces:
This is a string with spaces.
Lots of different spaces blah blah blah. Also a period.
This method allows for more complex processing of spaces in strings, such as removing spaces before periods or handling sequences of spaces efficiently.
The Python programming language offers a specific set of built-in tools for string manipulation — for example, operations with space characters:
Each variant has its own set of methods — most of which we have covered in this guide.
If you want to build a web service using Python, you can rent a cloud server at competitive prices with Hostman.