To replace multiple spaces with a single space in Python, you can use the `re` module and the `sub` function.
In this example,
re
module.\s+
to match multiple spaces.sub
function to replace multiple spaces with a single space in the string str
.str
.import re
str = "This is a test string"
modifiedStr = re.sub(r'\s+', ' ', str)
print(modifiedStr)
This is a test string
In this tutorial, we learned How to Replace multiple Spaces with a Single Space in String in Python language with well detailed examples.