In this tutorial, we will look at how to remove multiple spaces from a string in Python such that there is consistent spacing.
How do you remove multiple spaces in a string?
You can use regular expressions to match consecutive space characters in the string and replace them with a single space. The following is the syntax –
# replace multiple spaces with a single space s = re.sub(' +', ' ', s)
Let’s look at an example.
import re # string with multiple consecutive spaces s = "I am a doctor" # make spaces consistent s = re.sub(" +", " ", s) print(s)
Output:
I am a doctor
Multiple spaces were replaced with a single space. Here, the regular expression ' +'
matches with occurrences of consecutive spaces which are then replaced by a single space character, ' '
using the re.sub()
function.
The regular expression ' +'
matches for one or more consecutive occurrences of a single space ' '
.
Using sting split()
and join()
You can also use the string split()
and join()
functions to remove multiple spaces from a string.
We first split the string using the string split()
function and then join the words back with a single space between them using the string join()
function. For example –
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# string with multiple consecutive spaces s = "I am a doctor" # make spaces consistent s = " ".join(s.split()) print(s)
Output:
I am a doctor
We get the same result as above.
Note that the string split()
function splits the string at whitespace characters by default. Now, whitespace characters are not just limited to a single space in Python. Characters such as '\n'
, '\t'
, '\r'
etc. are also considered whitespace characters in Python.
If you use this method on a string with such characters, they will all be replaced by a single space. For example –
# string with multiple consecutive spaces s = "I am a doctor.\nHe is a scientist." # make spaces consistent s = " ".join(s.split()) print(s)
Output:
I am a doctor. He is a scientist.
You can see that multiple spaces were removed from the string. Also, notice that the newline character '\n'
was also removed.
Now, let’s use the regular expression method on the same string.
# string with multiple consecutive spaces s = "I am a doctor.\nHe is a scientist." # make spaces consistent s = re.sub(" +", " ", s) print(s)
Output:
I am a doctor. He is a scientist.
Only consecutive spaces were replaced by a single space and the newline character was unchanged.
So, be mindful of this when using string split()
and join()
functions to remove multiple spaces from a string in Python.
You might also be interested in –
- Remove Linebreaks From String in Python
- Python – Remove Non Alphanumeric Characters from String
- Remove Punctuation From String in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.