Python Practice › String Programs › Program 73

Remove leading zeros from an IP address

EasyString Programs · 73 of 106

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

Advertisement
Input
"289.03.02.054"
Output
289.3.2.54

Approach

  1. Split on dots, convert each part to int and back.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs