Python: Remove leading zeros from an IP address
16. Remove Leading Zeros from IP
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
Pictorial Presentation:
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to remove leading zeros from each octet of an IP address.
- Write a Python script to convert a zero-padded IP address into its canonical form without leading zeros.
- Write a Python program to validate an IP address and then strip any leading zeros from its segments.
- Write a Python program to reformat a given IP address by removing extra zeros and then print the updated address.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program where a string will start with a specific number.
Next: Write a Python program to check for a number at the end of a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.