You can remove more than 1 space (Unwanted space) between intermediate words in strings using re.sub() or split() + join() method.
Example remove multiple spaces from string Python
Simple example code.
Using the regex will unwanted space between the words to be just a single space using the appropriate regex string.
import re string1 = "The fox jumped over the log." res = re.sub(' +', ' ', string1) print(res)
Using split() and join()
string1 = "The fox jumped over the log." res = " ".join(string1.split()) print(res)
Output: The fox jumped over the log.
Do comment if you have any doubts and suggestions on this Python string topic.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.