w3resource
Python Exercises

Python: Abbreviate 'Road' as 'Rd.' in a given string

Python Regular Expression: Exercise-30 with Solution

Write a Python program to abbreviate 'Road' as 'Rd.' in a given string.

Sample Solution:-

Python Code:

import re
street = '21 Ramkrishna Road'
print(re.sub('Road$', 'Rd.', street))

Sample Output:

21 Ramkrishna Rd.  

Flowchart:

Flowchart: Regular Expression - Abbreviate 'Road' as 'Rd.' in a given string.

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 separate and print the numbers and their position of a given string.
Next: Write a Python program to replace all occurrences of space, comma, or dot with a colon.

What is the difficulty level of this exercise?



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