- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
Python program to remove leading zeros from an IP address
In this tutorial, we are going to write a program which removes leading zeros from the Ip address. Let's see what is exactly is. Let's say we have an IP address 255.001.040.001, then we have to convert it into 255.1.40.1. Follow the below procedure to write the program.
- Initialize the IP address.
- Split the IP address with. using the split function
- Convert each part of the IP address to int which removes the leading zeros.
- Join all the parts by converting each piece to str.
- The result is our final output.
## initializing IP address ip_address = "255.001.040.001" ## spliting using the split() functions parts = ip_address.split(".") ## converting every part to int parts = [int(part) for part in parts] ## convert each to str again before joining them parts = [str(part) for part in parts] ## joining every part using the join() method ip_address = ".".join(parts) print(ip_address)
If you run the above program, you will get the following result.
If you have any doubts regarding the program, please do mention them in the comment section.
- Related Articles
- Python program to remove leading 0's from an IP address
- Java Program to Remove leading zeros
- Golang program to remove leading zeros
- Remove Leading Zeros from an Array using C++
- Remove Leading Zeros From String in Java
- Remove Leading Zeros from a String in C#
- Add leading Zeros to Python Program string
- C Program to validate an IP address
- Remove leading zeros from a Number given as a string using Python
- Defanging an IP Address in Python
- Python program to Display Hostname and IP address?