How to Remove Spaces from String in Python: 3 Easy Methods

How to Remove Spaces from String in Python

Curious about how to remove spaces from string in Python? Let’s find out how.

How to Remove Spaces from String in Python?

Python provides programmers with several ways to remove spaces from a string. Here are a few:

  • Method 1: Using str.replace() method
  • Method 2: Using str.split() and str.join() methods
  • Method 3: Using List Comprehension 

Let’s go through each separately!

Method 1: Using str.replace() method

In Python, str.replace() is a method that returns a new string after replacing all occurrences of a specified substring with another mentioned substring. The first argument of this method is the substring to be replaced, and the second one is the new substring. We can utilize this method to remove space from a string as well.

Here is how:

  • First of all, define a string having some spaces.
  • Then, call the replace() method on the string while passing space as the first argument and an empty string as the second one.
  • Assign the result of the above expression in a variable and print it.
welcome_string = "Hey, welcome to our site"
no_spc = welcome_string.replace(" ", "")
print(no_spc)

removing spaces from a string using str.replace()

Method 2: Using str.split() and str.join() methods

The str.split() method is utilized to divide a string into a list of strings according to a particular delimiter. While the str.join() method is used to combine a list of strings into a single one.

This is how we can make use of these two methods for removing spaces from a string in Python:

  • Define an original string with spaces.
  • After that, apply the split() method on that string.
  • Call the join() method on an empty string while passing the split() method expression as an argument.
  • Lastly, store the combined string returned by the join() method to a variable and print it on the console.
welcome_string = "Hey, welcome!"
no_spc = ''.join(welcome_string.split())
print(no_spc)

removing spaces from a string using str.split() and str.join()

Note: If you pass no arguments to the split() method, it will operate further with its default delimiter (spaces, tabs, newlines).

Method 3: Using List Comprehension 

List comprehension is an easy and efficient way to create lists from existing iterables like strings.

Below is how this approach can be used to remove spaces from a string:

  • Initialize a string that contains a few spaces.
  • Use list comprehension to make a list of all characters in the mentioned string that is not a space.
  • Pass this expression to the join() method and call it on an empty string.
  • Define a new variable to hold the result from the above expression and print it.
welcome_string = "Hey there, welcome to our website!"
no_spc = ''.join([char for char in welcome_string if char != ' '])
print(no_spc)

removing spaces from a string using list comprehension

Conclusion

In Python, there are multiple ways to remove spaces from a string including list comprehension, str.replace() method, and split() and join() method. Each of these approaches is explained briefly with code.

Which one of these approaches is the easiest? Let me know in the comments.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Scroll to Top