The re module includes the sub function, which enables you to find any substring matching
a specified pattern and replace it with a substring of your choice.
For example, the following code replaces all occurrences of one or more consecutive whitespace
characters with a single space:
import re
patientname = "Patient Name: John Doe"
patientname = re.sub("\s+", " ", patientname)
# patientname is now "Patient Name: John Doe"
To specify an upper limit on the number of substitutions to perform, supply a count as the fourth parameter to sub:
import re
patientname = "Patient Name: John Doe"
patientname = re.sub("\s+", " ", patientname, 2)
# patientname is now "Patient Name: John Doe"