Python

Python Remove Spaces from String

The strings created in Python are immutable, so they cannot be changed. Python has a huge set of functions that can be used on the strings. However, all the string functions return the new value as they can’t change the original value of the string. Any function that manipulates the python string value returns a new string value which is assigned to a new string so that the original string can be changed. The most commonly used python string functions are capitalize(), split(), count(), find(), index(), join(), replace(), etc. Thus, to remove the white spaces from the string, various python string functions can be used. In this article, we will discuss the top 3 explicitly used methods to remove space from python strings. Suppose we have a string defined as:

# PyString = ‘This is Python String \n\t’

The PyString has four spaces, a new line ‘\n’ character along with a tab ‘\t’ character. Let’s see how the spaces can be removed from the given PyString. Below you can find the syntax and three examples of how to remove spaces from the string.

Example 1

As mentioned above, the replace() function is a commonly used python string function. The replace() function replaces a specific character, word, or phrase within a specified phrase. Here is the syntax of the replace() function that is used to remove spaces from the string.

# General syntax: variableName.replace(OldString, NewString, count)

The variableName is a variable that contains the string. OldString is a word, character, or phrase that needs to be changed, and the user must provide it. NewString is a new word, character, or phrase that should replace the old value in the string, and it must be provided. Lastly, the count specifies the number of occurrences of the old value that should be replaced. It is optional; hence, if the count is not provided, the default will replace all the occurrences in the OldString. Here is the code that replaces the space in the string:

PyString = 'This is Python String '

x = PyString.replace(" ", "")

print(x)

The output of the overhead code is as follows.

Example 2

The next example is a combination of two python string functions split() and join(). The split() function separates the string at the specified separator and returns the string as a list. The general syntax of the split() function is as follows:

# General syntax: variableName.split(separator, count)

The variableName is a variable that contains the string. The separator specifies the separator that will be used to split the string. It is optional; hence, the default value is whitespace. The count specifies the number of splits that need to be done. It is also optional; hence, the default value is “all occurrences”. On the contrary, the join() function takes the list and returns all the iterable values into a single string. Below is the syntax of the join() function:

# General syntax: variableName.join(list)

The variableName is a variable that contains the string. The list is an iterable list of items that will be returned as a single string, and it must be provided. Have a look at the split() and join() code to replace the spaces in the string.

PyString = 'This is Python String \n\t'
print("".join(PyString.split()))

The above code creates the following output.

Example 3

In the last example, we will discuss the use of the translate() function to remove the spaces from a string. The translate() function is comparable to the replace() function. The general purpose of the translate() function is to replace a specific character in the given string with the character defined in the mapping table. Here is the syntax of translate() function.

# General syntax: varibaleName.transalate(value)

The variableName is a variable that contains the string. The value is a mapping table or a dictionary character that defines how to perform the replacement. Below is the code of the translate() function to substitute the spaces in a string.

PyString = 'This is Python String \n\t'

mytable = PyString.maketrans('','',' \n\t\r')

print(PyString.translate(mytable))

Here is the result when we execute the above code.

Conclusion

There are numerous methods to eliminate the spaces from strings in Python. A few examples are provided above that are very easy to learn and used to remove the spaces from a string. However, the main key point that should be kept in mind is that python strings are immutable, so they can’t be changed, and a new string needs to be created to store the manipulated or changed string.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content