To remove spaces or whitespaces from a string in Python, we can use many ways. For example strip() method, replace() method, regex, etc. These are built-in methods of Python. Now, we will understand to remove the spaces from the string with the help of examples.
First, we will start by removing all the spaces at the beginning and end of the string.
The strip() is a method of String class in Python that is used to remove all the leading (start) and trailing(end) spaces from a string. We can see in the given example, we have a string that contains lots of spaces at the start and end of the string that increases the unnecessary size of the string. So, we used the strip() method to remove these spaces. We print the size of the string before and after removing the space and see, size is reduced.
# Take a String
str = " MyString ";
print(len(str)) # Display total length of string
# Remove all the spaces back and forth
str = str.strip();
# Display string
print(str)
print(len(str)) # Total length after strip
Output:
21
MyString
8
If we want to remove only leading (start) spaces from the string then we can use the lstrip() method of the Python. This method returns a string after removing spaces. We print the size of the string before and after removing the space and see, size is reduced.
# Take a String
str = " MyString ";
print(len(str)) # Display total length of string
# Remove all the spaces from left side of the string
str = str.lstrip();
# Display string
print(str)
print(len(str)) # Total length after strip
Output:
21
MyString
11
If we want to remove only trailing (end) spaces from the string then we can use the rstrip() method of the Python. This method returns a string after removing spaces. We print the size of the string before and after removing the space and see, size is reduced.
# Take a String
str = " MyString ";
print(len(str)) # Display total length of string
# Remove all the spaces from right side of the string
str = str.rstrip();
# Display string
print(str)
print(len(str)) # Total length after strip
Output:
21
MyString
18
Notice the output, spaces at the start of the string are still there because we removed only trailing(end) spaces.
We can use the replace() method to remove all the spaces from a string but it removes all the spaces including start, end, and between the string. The replace() method takes two arguments, first is what you want to replace, and second is to with want to replace. We print the size of the string before and after removing the space and see, size is reduced.
# Take a String
str = " MyString in Python ";
print(len(str)) # Display total length of string
# Remove all the spaces of the string
str = str.replace(" ","");
# Display string
print(str)
print(len(str)) # Total length after removing spaces
Output:
30
MyStringinPython
16
Whitespaces are newline(\n), tab(\t), etc that renders spaces in the space. If we want to remove whitespace then we can use the split() and join() method together. We print the size of the string before and after removing the space and see, size is reduced. Here is an example.
# Take a String
str = " MyString \n \t ";
print(len(str)) # Display total length of string
# Remove all the spaces and whitespaces of the string
str = ''.join(str.split())
# Display string
print(str)
print(len(str)) # Total length after removing spaces
Output:
24
MyString
8
If you want to use regex to remove spaces and whitespaces then use the Python regex module that returns a string without any space or whitespace. We print the size of the string before and after removing the space and see, size is reduced.
import re
# Take a String
str = " MyString \n \t in Python ";
print(len(str)) # Display total length of string
# Remove all the spaces and whitespaces of the string using regex
str = re.sub(r"\s+", "", str, flags=re.UNICODE)
# Display string
print(str)
print(len(str)) # Total length after removing spaces
Output:
34
MyStringinPython
16