w3resource

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.  

Pictorial Presentation:

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

Flowchart:

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

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?

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://www.w3resource.com/python-exercises/re/python-re-exercise-30.php