Strings in python are used extensively for processing text data. In this article, we will look at different ways with which we can remove whitespace characters from a string. We will also implement the examples to understand the concept in a better manner.
What are whitespace characters?
Whitespace characters are the characters such as spaces, tabs and newlines. In python, a string constant string.whitespace is defined that contains all the whitespace characters. These characters are spaces that are denoted by “ ”, tabs that are denoted by “\t”, newlines that are denoted by “\n”, carriage returns that are denoted by “\r”, vertical tabs that are denoted by “\v” and form feeds that are denoted by “\f”.
Now we will look at the different ways with which we can remove these whitespace characters from a string.
Remove whitespace characters using a for loop
The simplest way with which we can remove the whitespace characters from a string is to use a for loop. In this method, we will first create a new empty string. After that, for each character in the input string, we will check if it is a whitespace character or not. If yes, we will discard it. Otherwise, we will add the character to the newly created string using string concatenation operation as follows.
import string
input_string = """This is PythonForBeginners.com
Here, you can read python \v tutorials for free."""
new_string = ""
for character in input_string:
if character not in string.whitespace:
new_string = new_string + character
print("THe original string is:")
print(input_string)
print("Output String is:")
print(new_string)
Output:
THe original string is:
This is PythonForBeginners.com
Here, you can read python tutorials for free.
Output String is:
ThisisPythonForBeginners.comHere,youcanreadpythontutorialsforfree.
Remove whitespace characters using split() method
Instead of traversing each character of the input string, we can use the split() and the join() method to remove the whitespaces.
The split() method, when invoked on a string, splits the string at whitespaces and returns a list of the substrings. We can join all the substrings using the join() method.
The join() method, when invoked on a separator string, takes a list, tuple or other iterable object containing strings and joins all the strings in the iterable with the separator string. Here, we will use the empty string as the separator string. In this way, we can join all the substrings that were created by the split() method to create the output string.
import string
input_string = """This is PythonForBeginners.com
Here, you can read python \v tutorials for free."""
str_list = input_string.split()
new_string = "".join(str_list)
print("The original string is:")
print(input_string)
print("Output String is:")
print(new_string)
Output:
The original string is:
This is PythonForBeginners.com
Here, you can read python tutorials for free.
Output String is:
ThisisPythonForBeginners.comHere,youcanreadpythontutorialsforfree.
Remove whitespace characters using regular expressions
In python, we can use the sub() method from the regex module to substitute a pattern with another. The sub() method takes three input arguments. The first input is the pattern that needs to be replaced. The second input argument is the new pattern that has to be placed in the string. The third input argument is the input string. It returns a new string with the modified values.
Here, we will replace space characters in the with an empty string in the original string. The regular expression to match all the space characters is “\s+”. The new pattern will be just an empty string represented by “”. Using these patterns and the sub() method, we can remove the whitespace characters from an input string as follows.
import re
import string
input_string = """This is PythonForBeginners.com
Here, you can read python \v tutorials for free."""
new_string = re.sub(r"\s+", "", input_string)
print("The original string is:")
print(input_string)
print("Output String is:")
print(new_string)
Output:
The original string is:
This is PythonForBeginners.com
Here, you can read python tutorials for free.
Output String is:
ThisisPythonForBeginners.comHere,youcanreadpythontutorialsforfree.
Remove whitespace characters using a translation table
Instead of the regular expressions, we can also use a translation table and the translate() method to remove all the whitespace characters from a string.
A translation table is just a dictionary containing a mapping of old characters to new characters. In our program, we will map each space character to an empty string. Here, we will use the ASCII values of the space characters as keys and empty string as their associated value. The ASCII value of each character can be found using the ord() function. The translation table can be created as follows.
import string
translation_table = {ord(x): "" for x in string.whitespace}
print("The translation table is:")
print(translation_table)
Output:
The translation table is:
{32: '', 9: '', 10: '', 13: '', 11: '', 12: ''}
After creating the translation table, we can use the translate() method to remove the whitespace characters from the input string. The translate() method, when invoked on a string,takes the translation table as input and replaces the characters in it using the translation table.
We will use the translation table created above to replace the whitespace characters with empty string as follows.
import string
input_string = """This is PythonForBeginners.com
Here, you can read python \v tutorials for free."""
translation_table = {ord(x): "" for x in string.whitespace}
new_string = input_string.translate(translation_table)
print("The original string is:")
print(input_string)
print("Output String is:")
print(new_string)
Output:
The original string is:
This is PythonForBeginners.com
Here, you can read python tutorials for free.
Output String is:
ThisisPythonForBeginners.comHere,youcanreadpythontutorialsforfree.
Conclusion
In this article, we have discussed different ways to remove whitespace characters from a string. To learn more about python programming, you can read this article on list comprehension. You may also like this article on the linked list in Python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.