w3resource logo
Python Exercises

Python: Remove leading zeros from an IP address

Python Regular Expression: Exercise-16 with Solution

Write a Python program to remove leading zeros from an IP address.

Sample Solution:-

Python Code:

import re
ip = "216.08.094.196"
string = re.sub('\.[0]*', '.', ip)
print(string)

Sample Output:

216.8.94.196 

Python Code Editor:

import re
ip = "216.08.094.196"
string = re.sub('\.[0]*', '.', ip)
print(string)

Improve this sample solution and post your code through Disqus