Report this

What is the reason for this report?

Effective Ways to Remove Spaces from Strings in Python

Updated on July 11, 2025
Effective Ways to Remove Spaces from Strings in Python

Introduction

In this tutorial, you will learn various methods to remove whitespace from a string in Python. Removing spaces from strings is a frequent task in Python programming, especially when working with user input, data cleaning, or text processing. Strings often contain unwanted whitespace that can lead to errors in data processing, validation, and comparisons. Properly handling these characters ensures data consistency and integrity.

This process involves removing various types of whitespace characters, which include not just the standard space (' ') but also tabs (\t), newlines (\n), and carriage returns (\r). Often, you may also need to handle multiple, consecutive spaces between words or leading and trailing spaces around the entire string.

In real-world scenarios, space removal is essential for:

  • Cleaning user input: Stripping extra spaces from usernames, passwords, or form fields before saving them to a database.
  • Parsing data files: Removing whitespace and newlines from data read from CSV, TXT, or JSON files to ensure accurate parsing.
  • Comparing strings: Ensuring that two strings, like 'hello world' and ' hello world ', are treated as equal after normalization.

It is important to remember that Python strings are immutable, meaning their values cannot be changed after they are created. Therefore, any method that manipulates a string will return a new string with the desired modifications. This tutorial will cover different techniques, including built-in string methods and regular expressions, to effectively remove whitespace from your strings.

Deploy your Python applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

The examples in this tutorial use the Python interactive console in the command line to demonstrate different methods that remove spaces. The examples use the following string:

s = '  Hello  World   From DigitalOcean \t\n\r\tHi There  '

The output is:

Output
Hello World From DigitalOcean Hi There

This string has different types of whitespace and newline characters, such as space (' '), tab (\t), newline (\n), and carriage return (\r).

Key Takeaways

  • Use the strip() method and its variants, lstrip() and rstrip(), to efficiently remove whitespace from only the beginning or end of a string.
  • Combine the join() and split() methods to normalize a string by collapsing multiple, varied whitespace characters into a single space between words.
  • For the fastest performance when removing all types of whitespace characters, use the translate() method with the string.whitespace constant.
  • Reserve regular expressions with re.sub() for complex, pattern-based whitespace removal that simpler, faster string methods cannot accomplish.
  • Remember that Python strings are immutable, meaning every removal method returns a new, modified string instead of altering the original.

Remove Leading and Trailing Spaces Using the strip() Method

The Python String strip() method removes leading and trailing characters from a string. The default character to remove is space.

Declare the string variable:

  1. s = ' Hello World From DigitalOcean \t\n\r\tHi There '

Use the strip() method to remove the leading and trailing whitespace:

  1. s.strip()

The output is:

Output
'Hello World From DigitalOcean \t\n\r\tHi There'

If you want to remove only the leading spaces or trailing spaces, then you can use the lstrip() and rstrip() methods.

Remove All Spaces Using the replace() Method

You can use the replace() method to remove all the whitespace characters from the string, including from between words.

Declare the string variable:

  1. s = ' Hello World From DigitalOcean \t\n\r\tHi There '

Use the replace() method to replace spaces with an empty string:

  1. s.replace(" ", "")

The output is:

Output
'HelloWorldFromDigitalOcean\t\n\r\tHiThere'

Note that this method only removes the standard space character (' ') and will not remove other whitespace like tabs (\t) or newlines (\n).

Remove Duplicate Spaces and Newline Characters Using the join() and split() Methods

You can remove all of the duplicate whitespace and newline characters by using the join() method with the split() method. In this example, the split() method breaks up the string into a list, using the default separator of any whitespace character. Then, the join() method joins the list back into one string with a single space (" ") between each word.

Declare the string variable:

  1. s = ' Hello World From DigitalOcean \t\n\r\tHi There '

Use the join() and split() methods together to remove duplicate spaces and newline characters:

  1. " ".join(s.split())

The output is:

Output
'Hello World From DigitalOcean Hi There'

Remove All Spaces and Newline Characters Using the translate() Method

You can remove all of the whitespace and newline characters using the translate() method. The translate() method replaces specified characters with characters defined in a dictionary or mapping table. The following example uses a custom dictionary with the string.whitespace string constant, which contains all the whitespace characters. The custom dictionary {ord(c): None for c in string.whitespace} replaces all the characters in string.whitespace with None.

Import the string module so that you can use string.whitespace:

  1. import string

Declare the string variable:

  1. s = ' Hello World From DigitalOcean \t\n\r\tHi There '

Use the translate() method to remove all whitespace characters:

  1. s.translate({ord(c): None for c in string.whitespace})

The output is:

Output
'HelloWorldFromDigitalOceanHiThere'

Remove Whitespace Characters Using Regex

You can also use a regular expression to match whitespace characters and remove them using the re.sub() function.

This example uses the following file, regexspaces.py, to show some ways you can use regex to remove whitespace characters:

regexspaces.py
import re

s = '  Hello  World   From DigitalOcean \t\n\r\tHi There  '

print('Remove all spaces using regex:\n', re.sub(r"\s+", "", s), sep='')  # \s matches all white spaces
print('Remove leading spaces using regex:\n', re.sub(r"^\s+", "", s), sep='')  # ^ matches start
print('Remove trailing spaces using regex:\n', re.sub(r"\s+$", "", s), sep='')  # $ matches end
print('Remove leading and trailing spaces using regex:\n', re.sub(r"^\s+|\s+$", "", s), sep='')  # | for OR condition

Run the file from the command line:

python3 regexspaces.py

You get the following output:

Remove all spaces using regex:
HelloWorldFromDigitalOceanHiThere
Remove leading spaces using regex:
Hello  World   From DigitalOcean  
 Hi There  
Remove trailing spaces using regex:
  Hello  World   From DigitalOcean  
 Hi There
Remove leading and trailing spaces using regex:
Hello  World   From DigitalOcean  
 Hi There

Performance Comparison: Which Method is Fastest?

When removing whitespace from strings, especially in performance-critical applications or when processing large volumes of text, the efficiency of the method you choose matters. This section compares the performance of the primary methods discussed and provides guidance on when to use each one.

We will use Python’s built-in timeit module to benchmark the replace(), join()/split(), translate(), and re.sub() methods.

Benchmarking with timeit

To compare the speeds, we can run each operation multiple times on a sample string and measure the total execution time.

This example uses the following file, benchmark.py, to test the performance of each method for removing all whitespace characters:

benchmark.py
import timeit
import re
import string

s = '  Hello  World   From DigitalOcean \t\n\r\tHi There  ' * 1000
iterations = 10000

def method_replace():
    return s.replace(' ', '')

def method_join_split():
    return "".join(s.split())

def method_translate():
    return s.translate({ord(c): None for c in string.whitespace})

def method_regex():
    return re.sub(r"\s+", "", s)

print(f"replace(): {timeit.timeit(method_replace, number=iterations)}")
print(f"join(split()): {timeit.timeit(method_join_split, number=iterations)}")
print(f"translate(): {timeit.timeit(method_translate, number=iterations)}")
print(f"regex(): {timeit.timeit(method_regex, number=iterations)}")

Running this file from the command line will produce timing results. The exact numbers will vary based on your system, but the relative performance is generally consistent.

A typical output might look like this:

Output
replace(): 0.0152164
join(split()): 0.0489321
translate(): 0.0098745
regex(): 0.1245873

From these results, we can observe:

  • translate() is typically the fastest method for removing all types of whitespace.
  • replace() is very fast for removing a single, specific character, but it cannot remove varied whitespace types in one call.
  • join(split()) is slower because it involves two distinct operations: creating a list of substrings and then joining them back together.
  • re.sub() (regular expressions) is the slowest due to the overhead of the regex engine.

Memory Efficiency and Use Cases

Beyond speed, memory usage is another important consideration, especially for very large strings.

  • replace() and translate(): These methods are generally memory-efficient as they create a single new string as the result.
  • join(split()): This method can be memory-intensive for strings with many words, as split() first creates a list containing all the substrings. This intermediate list can consume significant memory.
  • re.sub(): The memory usage of regex can vary, but it is generally less efficient than direct string methods for simple substitution tasks.

When to Use Each Method

Based on performance and functionality, here is a guide to choosing the right method:

  • For removing only leading and trailing whitespace: Use strip(), lstrip(), or rstrip(). They are fast, clear, and designed specifically for this purpose.
  • For removing ALL whitespace characters (including spaces, tabs, newlines): Use translate(). It is the fastest and most efficient method for this task.
  • For collapsing all whitespace into a single space between words: Use " ".join(s.split()). While not the fastest overall, it is the most direct and readable way to achieve this specific normalization.
  • For complex pattern-based removal (e.g., removing spaces only between numbers): Use re.sub(). Although it is the slowest, its power and flexibility are unmatched for tasks that cannot be handled by basic string methods.

By choosing the appropriate method based on your specific requirements, you can ensure your code is not only correct but also efficient.

Common Pitfalls & Best Practices

When removing whitespace, it’s easy to introduce subtle bugs if you’re not careful. This section covers common pitfalls to avoid and best practices to follow for writing robust and efficient code.

Preserving Intentional Spaces in Formatted Text

A common mistake is removing all spaces from a string when you only intend to clean up the edges or extra spacing. For example, applying replace(' ', '') to a sentence will remove the necessary spaces between words.

Best Practice: Before choosing a method, determine if the internal spaces are meaningful. If you only need to remove leading and trailing whitespace, always use strip(). If you need to normalize inconsistent spacing between words, use " ".join(s.split()).

# Pitfall: Removing necessary spaces
formatted_string = "   Product ID: 123-456   "
print(formatted_string.replace(' ', ''))
# Output: 'ProductID:123-456' -> Data is now corrupted

# Best Practice: Use strip() to preserve internal spaces
print(formatted_string.strip())
# Output: 'Product ID: 123-456'

Handling None Values and Edge Cases

Applying a string method to a variable that might be None or a non-string type (like a number) will raise an AttributeError. This can crash your application if not handled properly.

Best Practice: Always validate your input before attempting to manipulate it. You can do this by checking if the variable is not None or by wrapping the operation in a try-except block.

# Pitfall: Calling a method on a None value
user_input = None

# Best Practice: Check for None before processing
if user_input is not None:
    cleaned_input = user_input.strip()
else:
    cleaned_input = ""
print(f"Cleaned Input: '{cleaned_input}'")

Performance Optimization Tips

As shown in the performance comparison, not all methods are equally fast. Using a slow method on a large dataset or in a frequently called function can create a performance bottleneck.

Best Practice: Choose the most efficient tool for the job. For removing all whitespace characters, translate() is the fastest option. For simple leading/trailing space removal, strip() is highly optimized. Avoid using regular expressions (re.sub()) for simple whitespace removal, as it is significantly slower.

Code Readability vs. Efficiency Trade-offs

While translate() is the most performant method for removing all whitespace, its syntax can be less intuitive to new developers compared to a method like replace().

Best Practice: Prioritize code readability unless you are working in a performance-critical context. For most day-to-day scripting, a slightly slower but more understandable method is often a better choice.

  • Readable but slower: " ".join(s.split()) is very clear about its intent to normalize spaces.
  • Efficient but less readable: s.translate({ord(c): None for c in string.whitespace}) is faster but requires understanding dictionaries and the translate method.

If performance becomes an issue, profile your code first to identify the bottleneck, and only then replace a readable method with a more performant one, adding a comment to explain the logic.

FAQs

1. How do you remove spaces from a string in Python?

There are several ways, depending on which spaces you want to remove:

  • To remove all spaces: Use replace():
my_string = "Hello World"
no_spaces = my_string.replace(" ", "")
# no_spaces is now "HelloWorld"
  • To remove leading and trailing spaces only: Use strip():
my_string = "  Hello World  "
trimmed = my_string.strip()
# trimmed is now "Hello World"
  • To remove spaces using a regular expression (to handle multiple whitespace types):
import re
my_string = "Hello   World"
no_spaces = re.sub(r"\s+", "", my_string)
# no_spaces is now "HelloWorld"

2. How to remove spaces in string?

To remove all spaces, use my_string.replace(" ", ""). To remove only leading and trailing spaces, use my_string.strip().

3. What does strip() do in Python?

The strip() method returns a new string by removing all leading (at the start) and trailing (at the end) whitespace characters. For example:

my_string = "   Hello World   "
trimmed = my_string.strip()  
# trimmed = "Hello World"

4. How do you remove spaces trim in Python string?

To “trim” spaces—meaning to remove them only from the start and end of the string—use the strip() method:

my_string = "   Trim me   "
trimmed = my_string.strip()  
# trimmed = "Trim me"

5. What is stripping whitespace in Python?

“Stripping whitespace” refers to removing any leading and trailing whitespace characters (including spaces, tabs, and newlines) from a string. The strip(), lstrip(), and rstrip() methods are commonly used for this purpose.

6. How do I remove part of a string in Python?

To remove a known substring from a string, you can use replace():

my_string = "Hello World"
removed_part = my_string.replace("World", "")
# removed_part = "Hello "

If you need to remove content by index, you can use slicing:

my_string = "Hello World"
# Remove "lo Wo"
removed_part = my_string[:3] + my_string[8:]
# removed_part = "Hello d"

7. Which method is used to remove whitespace?

The strip() method is used to remove whitespace from the start and end of a string. For removing whitespace from the entire string, replace() can be used, and for more sophisticated patterns, you can use regular expressions via the re module.

8. How to remove space in Python print?

When using print() with multiple arguments, Python adds a space by default. To avoid this, you can specify the sep parameter:

print("Hello", "World", sep="")
# Outputs: "HelloWorld"

If your string already contains spaces you want to remove, apply .replace(" ", "") or strip() before printing:

my_string = "Hello World"
print(my_string.replace(" ", ""))
# Outputs: "HelloWorld"

Conclusion

In this tutorial, you learned various methods to remove whitespace characters from strings in Python. These methods include using the strip(), replace(), join() and split(), translate(), and regular expressions. Each method has its own use case and can be chosen based on the specific requirements of your task.

To further enhance your understanding of string manipulation in Python, you can refer the following tutorials:

By using these resources, you can deepen your knowledge and become more proficient in handling strings in Python.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Pankaj Kumar
Pankaj Kumar
Author
See author profile

Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev

Anish Singh Walia
Anish Singh Walia
Editor
Sr Technical Writer
See author profile

Helping Businesses stand out with AI, SEO, & Technical content that drives Impact & Growth | Senior Technical Writer @ DigitalOcean | 2x Medium Top Writers | 2 Million+ monthly views & 34K Subscribers | Ex Cloud Engineer @ AMEX | Ex SRE(DevOps) @ NUTANIX

Manikandan Kurup
Manikandan Kurup
Editor
Senior Technical Content Engineer I
See author profile

With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.

Category:

Still looking for an answer?

Was this helpful?

I don’t see anymore the date things were published. Why is it removed? Regards

- Valentino

You really need to point out that the functions create and return a new string, they do not change the original. Yes, strings being immutable is a foundational Python concept but someone who’s looking for this level of help is probably very new to Python. It can be incredibly frustrating for a beginner to find a page like this and do: s = " My string with leading/trailing spaces ’ s.strip() print(s) What the heck? It doesn’t work!

- DJ

let me explain you with an simple example s= (" PYTHON “) #so here i am using both and leading and trailing spaces output= s.strip(” “) # in between double quotes i am using space as i have used space before and after PYTHON print(output) # you will get output as “PYTHON” … another example while using idle --------------------------------------- >>> s= (” python ") >>> s.strip() ‘python’ >>> see both the trailing and leading spaces have been removed

- DEEPAK

This was so helpful! Thanks for putting this together! :)

- Azmain Nisak

the replace function helped me where i was thinking of a more complex solution. So thank you

- Munir

greeting= “Hello” user= “Guy” message= “welcome to the thunderdome Friend” print(greeting.upper(), user.capitalize(), message.strip( ).lower() ) ive also tried print(greeting.upper(), user.capitalize(), message.replace(" ", " ").lower() ) goal is to lowercase friend and get rid of white space. when I run the .py file through cmd it just returns HELLO Guy welcome to the thunderdome friend no matter what I seem to try

- Pegel

Very helpful article, Pankaj. Thank you for this

- ypll

hey nice one thank you helped me in my code

- Moulya

Thank you so much.

- Sai Vinay Palakodeti

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.