w3resource

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


30. Abbreviate Road

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.

For more Practice: Solve these Related Problems:

  • Write a Python program to replace every occurrence of the word "Road" with "Rd." in a text.
  • Write a Python script to abbreviate long street names in an address by converting "Road" to "Rd." using regex.
  • Write a Python program to search for the term "Road" in a file and replace it with its abbreviation, then output the modified text.
  • Write a Python program to identify and abbreviate the word "Road" in a string only if it appears at the end of an address line.

Go to:


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.

Python Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.