• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / How to Remove All Spaces from String in Python

How to Remove All Spaces from String in Python

August 24, 2022 Leave a Comment

To remove all spaces from a string in Python, the easiest way is with the Python string replace() function.

string = "This is a string with spaces."

print(string.replace(" ",""))

#Output:
Thisisastringwithspaces.

One other easy way to remove all spaces from a string is with regex and the re module.

import re 

string = "This is a string with spaces."

print(re.sub('\s+',' ',string))

#Output:
Thisisastringwithspaces.

When using string variables in Python, the ability to easily be able to modify and change the value of these variables is important.

One such case is if you want to remove specific characters from a string.

To remove all spaces from a string in Python, the easiest way is with the Python string replace() function.

replace() takes a string to search for and a replacement value and replacing all found strings with the replacement value.

To remove all spaces from a string in Python, you can search for spaces and replace them with an empty string.

Below shows you how to use replace() to remove all spaces in a string in Python.

string = "This is a string with spaces."

print(string.replace(" ",""))

#Output:
Thisisastringwithspaces.

Removing All Spaces from String using Regex with Python

One other way you can remove all spaces from a string is with regex. To perform regex substitution, we can use the Python re module sub() function.

We can easily define a regular expression which will search for any number of spaces, and then using the sub() function, we will replace the spaces with an empty string.

Below are some examples of how you can remove all spaces from strings using Python with the sub() function.

import re 

string = "This is a string with spaces."

print(re.sub('\s+',' ',string))

#Output:
Thisisastringwithspaces.

Hopefully this article has been useful for you to learn how to remove all spaces from a string in Python.

Other Articles You'll Also Like:

  • 1.  Python getsizeof() Function – Get Size of Object
  • 2.  How to Remove All Occurrences of a Character in a List Using Python
  • 3.  Subtract Seconds from Datetime Variable Using Python timedelta() Function
  • 4.  Open Multiple Files Using with open in Python
  • 5.  Python asinh – Find Hyperbolic Arcsine of Number Using math.asinh()
  • 6.  Combine Sets in Python
  • 7.  Python isprime – Function for Determining if Number is Prime
  • 8.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 9.  How to Draw a Triangle in Python Using turtle Module
  • 10.  Using Python to Compare Strings Alphabetically

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy