w3resource logo
Python Exercises

Python: Replace whitespaces with an underscore and vice versa

Python Regular Expression: Exercise-23 with Solution

Write a Python program to replace whitespaces with an underscore and vice versa.

Sample Solution:-

Python Code:

import re
text = 'Python Exercises'
text =text.replace (" ", "_")
print(text)
text =text.replace ("_", " ")
print(text)

Sample Output:

Python_Exercises                                                                                              
Python Exercises

Python Code Editor:

import re
text = 'Python Exercises'
text =text.replace (" ", "_")
print(text)
text =text.replace ("_", " ")
print(text)

Improve this sample solution and post your code through Disqus