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.
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.
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:
x = PyString.replace(" ", "")
The output of the overhead code is as follows.
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:
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:
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.
The above code creates the following output.
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.
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.
mytable = PyString.maketrans('','',' \n\t\r')
Here is the result when we execute the above code.
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.