avatarYang Zhou

Summary

This article discusses five different methods to remove spaces in a messy Python string based on different scenarios and requirements.

Abstract

The article "5 Ways To Remove Spaces of a Python String" introduces five methods to remove unnecessary or duplicate spaces in Python strings. The simplest case involves using the strip() function to remove leading and trailing spaces. The replace() function can be used to remove all spaces, while the translate() function offers another way to achieve the same result. To eliminate duplicate spaces, the split() and join() functions can be used together. For more complex cases, regular expressions (RegEx) can be employed as the ultimate weapon for string manipulations. The article emphasizes the flexibility and elegance of Python in handling strings, especially messy ones, and the immutability of strings in Python.

Opinions

  • Handling strings, especially messy strings, is a common task for Python developers and data scientists.
  • Python provides various methods to remove spaces from strings, making it a flexible and elegant language for string manipulation.
  • Strings are immutable in Python, so the original string is not changed when spaces are removed.
  • Regular expressions (RegEx) are the ultimate weapon for handling complex string manipulations.
  • The article provides practical examples and code snippets to illustrate each method.
  • The article assumes familiarity with Python and some of its built-in functions.
  • The article encourages readers to follow the author for more great coding tutorials.

5 Ways To Remove Spaces of a Python String

From simple methods to the ultimate weapon

Handling strings, especially messy strings, is the daily work of Python developers and data scientists. One of the most common symptoms of a messy string is that there are unnecessary or duplicate spaces in it.

Fortunately, no matter how messy a string is and how complicated a scenario is, Python can provide us a corresponding way to remove spaces as we like.

This article will introduce 5 different methods to remove spaces in a messy Python string based on different scenarios and requirements. After reading, you can choose the most appropriate way for your code. More importantly, you will feel the flexibility and elegance of Python again.

1. Remove Leading and Trailing Spaces of a Python String

The simplest case is that all the leading and trailing spaces of a string are unnecessary. For this, we can just use the strip() function to remove them all.

For example:

The above code shows how easy it is to use the strip() function. There is one thing worth to mention: Strings are immutable in Python. The original quote string was not changed. We need to assign the result to a new string after removed the spaces.

In addition, there are two variants of the strip() function:

  • lstrip(): only remove the leading spaces of a string
  • rstrip(): only remove the trailing spaces of a string

2. Remove All Spaces of a Python String

Since removing spaces of a string is the same as replacing every space by an empty character, we can use the replace() function to do this job. This is a common method for cases that we need to remove all spaces of a string.

3. Another Way To Remove All Spaces of a Python String

Python is very flexible and there is another choice for us to do the same job as the replace() function — the translate() function.

The above code shows the usage of this function. If it looks intuitive at the first glance, no worries at all. We just need to know the two rules of its argument:

  • It must be a dictionary or a mapping table.
  • If it’s a dictionary, we must use ASCII rather than characters.

The second rule is easy to follow. We can just use the built-in ord() function to convert a character into its corresponding ASCII.

The first rule gives us more flexibility. If we need to change many characters instead of just spaces, writing the ord() many times may be annoying. In this case, we can use the maketrans() function to generate a mapping table from the original characters at first:

4. Remove Duplicate Spaces of a Python String

Sometimes, we need to eliminate duplicate spaces of a string instead of removing all spaces. In this scenario, we can use the following two functions together:

  • split(): To split a string into a list based on a separator (any whitespace by default)
  • join(): To combine all items in a list into a string using a separator

As the above example shows, the idea is clear:

  1. Split all words into a list. So the duplicate spaces are avoided.
  2. Combine all words again by the expected separator — ' '.

5. Use Regular Expressions To Remove Spaces as You Like

The previous four ways are useful enough for many simple scenarios. However, real data may be very complicated and messy. The previous ways cannot handle all cases. (If you are a data scientist and cleaning messy data is part of your job, you will agree with me. 🙂)

Therefore, we sometimes have to use the ultimate weapon for string manipulations — the regular expression (RegEx).

If you are familiar with RegEx, all previous four examples can be handled by it easily. For more complicated cases, RegEx will sort them out as well.

For example, if we need to remove any two consecutive spaces in a string, but keep all single spaces, the previous four ways may be hard to help. But the RegEx, as the ultimate weapon, can do us a favour:

Conclusion

Removing spaces of a Python string is a common task for developers. For some simple scenarios, some built-in functions of Python are useful enough. For more complicated cases, the RegEx is the ultimate weapon.

Thanks for reading. If you like it, please follow me to enjoy more great coding tutorials.

Relative articles:

Python
Programming
Data Science
Technology
Coding
Recommended from ReadMedium