Python Practice › String Programs › Program 73
Remove leading zeros from an IP address
Write a Python program to remove leading zeros from each part of an IP address.
Input
"289.03.02.054"
Output
289.3.2.54
Approach
- Split on dots, convert each part to int and back.
Solution
ip = "289.03.02.054"print(".".join(str(int(part)) for part in ip.split(".")))Output
289.3.2.54
Line by line
Line 2
int("054") is 54, which drops the zeros; int("0") stays 0.
Learn the concept
New to this topic? Read the lesson, then come back.