• 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 / Using Python to Replace Multiple Spaces with One Space in String

Using Python to Replace Multiple Spaces with One Space in String

February 15, 2022 Leave a Comment

To replace multiple spaces with one space using Python, the easiest way is to use the Python sub() function from the re module.

import re

string_multiple_spaces = "This   is    a string      with    some   multiple    spaces."

string_single_space = re.sub('\s+',' ',string_multiple_spaces)

print(string_single_space)

#Output:
This is a string with some multiple spaces.

Another method to replace multiple spaces with one space is combining the use of split() and join.

string_multiple_spaces = "This   is    a string      with    some   multiple    spaces."

string_single_space = " ".join(string_multiple_spaces.split())

print(string_single_space)

#Output:
This is a string with some multiple spaces.

When using string variables in Python, we can easily perform string manipulation to change the value of the string variables.

One such manipulation is to remove characters from a string variable. Multiple spaces between words in a string of text can make sentences and text unreadable.

We can easily replace multiple spaces with single spaces in a string in Python.

The easiest way to get rid of multiple spaces in a string is with a regular expression search using the Python sub() function from the re module.

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 multiple spaces with one space.

Below are some examples of how you can replace multiple spaces with one space in strings using Python with the sub() function.

import re

string_multiple_spaces = "This   is    a string      with    some   multiple    spaces."

string_single_space = re.sub('\s+',' ',string_multiple_spaces)

print(string_single_space)

#Output:
This is a string with some multiple spaces.

Using join() and split() to Replace Multiple Spaces with One Space in Python

Another way you can replace multiple spaces with one space in Python is by combing the join() and split() functions.

By default, the split() function splits with space as delimiter. When there are multiple spaces, those extra spaces are ignored.

Therefore, we can use the split() function to get a list of words in the string, and then use the join() method to join the words together with a single space.

Below is an example of how to use the join() and split() functions in Python to replace multiple spaces with single spaces.

string_multiple_spaces = "This   is    a string      with    some   multiple    spaces."

string_single_space = " ".join(string_multiple_spaces.split())

print(string_single_space)

#Output:
This is a string with some multiple spaces.

Hopefully this article has been useful for you to learn how to replace multiple spaces with one space using Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Read File Word by Word
  • 2.  Python Check if Attribute Exists in Object with hasattr() Function
  • 3.  Get Python Dictionary Keys as List
  • 4.  Get Current Year in Python
  • 5.  Python Get Number of Cores Using os cpu_count() Function
  • 6.  How to Ask a Question in Python
  • 7.  Python gethostbyname() Function – Get IPv4 Address from Name
  • 8.  Format Numbers as Dollars in Python with format()
  • 9.  Split Column by Delimiter in pandas DataFrame
  • 10.  Length of Tuple Python – Find Tuple Length with Python len() Function

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