w3resource

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

Pictorial Presentation:

Python: Regular Expression - Replace whitespaces with an underscore and vice versa.
Python: Regular Expression - Replace whitespaces with an underscore and vice versa.

Flowchart:

Flowchart: Regular Expression - Replace whitespaces with an underscore and vice versa.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the occurrence and position of the substrings within a string.
Next: Write a Python program to extract year, month and date from a an url.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/python-exercises/re/python-re-exercise-23.php