Python program to remove spaces from string

12/3/2023

#Python strip() method for trimming leading and trailing spaces #Python #program #remove_spaces from string

Go Back

Python Program to Remove Spaces from a String | Best Methods Explained

Introduction

This tutorial provides examples of various methods you can use to remove whitespace from a string in Python.

Removing spaces from strings in Python is a common task, especially when cleaning data or formatting text for output. In this tutorial, we will explore multiple methods to remove spaces from a string in Python, including both manual and built-in approaches.

By the end of this guide, you will learn:
✅ How to remove spaces from a string using a loop.
✅ How to use Python's built-in methods like replace() and strip().
✅ Best practices for handling whitespace in Python.

#Python strip() method for trimming leading and trailing spaces  #Python #program  #remove_spaces from string

Method 1: Remove Spaces Without Using Built-in Methods

You can manually remove spaces by looping through the string and building a new one without spaces.

# Input from the user
string = input("Enter a String: ")  
result = ''  

# Loop through the string
for i in string:  
    if i != ' ':  # If the character is not a space, add it to result
        result += i  

print("String after removing spaces:", result)  

Explanation:

  • The program takes user input.
  • It iterates through each character, checking if it's a space.
  • If it's not a space, the character is added to result.
  • Finally, the modified string without spaces is printed.

Method 2: Remove Spaces Using the replace() Method

Python provides a built-in method, replace(), to remove spaces efficiently.

# Define a function to remove spaces
def remove_spaces(str1):  
    return str1.replace(' ', '')  # Replace all spaces with an empty string  

# Test cases
print(remove_spaces("w 3 res ou r ce"))  
print(remove_spaces("a b c"))  

Explanation:

  • The replace(' ', '') function replaces all spaces in the string with an empty string.
  • It returns the modified string without spaces.
  • This method is faster and more efficient than manually iterating through the string.

Alternative Methods to Remove Spaces in Python

1. Using List Comprehension and join()

string = "Python is fun!"
result = "".join([char for char in string if char != ' '])
print(result)  # Output: Pythonisfun!

2. Using strip() to Remove Leading and Trailing Spaces

string = "   Hello, World!   "
print(string.strip())  # Output: "Hello, World!"

Conclusion

In this guide, we explored multiple ways to remove spaces from a string in Python:

  • Loop-based method for step-by-step removal.
  • Built-in replace() method for quick and efficient space removal.
  • List comprehension and join() for a concise approach.
  • strip() method for leading and trailing space removal.

These techniques are essential for data cleaning, text processing, and formatting output in Python. 🚀 Try them out in your Python programs!

Table of content