w3resource
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

Flowchart:

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

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

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 an url.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming


bedava porno