Python: Replace maximum 2 occurrences of space, comma, or dot with a colon
32. Replace Max 2 Delimiters
Write a Python program to replace maximum 2 occurrences of space, comma, or dot with a colon.
Sample Solution:
Python Code:
import re
text = 'Python Exercises, PHP exercises.'
print(re.sub("[ ,.]", ":", text, 2))
Sample Output:
Python:Exercises: PHP exercises.
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to replace at most two occurrences of space, comma, or dot in a string with a colon.
- Write a Python script to substitute a maximum of two delimiters in a string and then print the resulting string.
- Write a Python program to scan a string and replace only the first two occurrences of any delimiter with a colon.
- Write a Python program to perform a controlled replacement of delimiters in a text, changing only the first two instances.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to replace all occurrences of space, comma, or dot with a colon.
Next: Write a Python program to find all five characters long word in a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.