Given an IP address, remove leading zeros from the IP address.
Input : 100.020.003.400 Output : 188.8.131.520 Input :001.200.001.004 Output : 184.108.40.206
Method 1 : Traversal and join
The approach is to split the given string by “.” and then convert it to an integer which removes the leading zeros and then join back them to a string.To convert a string to an integer we can use int(s) and then convert it back to string by str(s) and then join them back by using join function.
Method 2 : Regex
Using a capture group, match the last digit and copy it and prevents all the digits from being replaced.
regex \d can be explained as:
- \d : Matches any decimal digit
\d Matches any decimal digit, this is equivalent to the set class [0-9].
- \b allows you to perform a “whole words only” search using a regular expression in the form of \bword\b
regex \b can be explained as :
\b allows you to perform a "whole words only" search u sing a regular expression in the form of \bword\b
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Remove leading zeros from a Number given as a string
- Python | Add leading Zeros to string
- Python | Remove trailing/leading special characters from strings list
- Python - Remove leading 0 from Strings List
- Python | Add leading K character
- PyQt5 QSpinBox - Getting the leading value of the font
- C program to trim leading white spaces from String
- numpy.zeros() in Python
- Python | sympy.zeros() method
- Python | Add trailing Zeros to string
- Create a Numpy array filled with all zeros | Python
- numpy.matlib.zeros() function | Python
- Python PyTorch zeros() method
- Python program to find IP Address
- Display Hostname and IP address in Python
- Extracting MAC address using Python
- Check if email address valid or not in Python
- Python program to validate an IP Address
- Python script to change MAC address of Linux machine
- Python program to determine if the given IPv4 Address is reserved using ipaddress module
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.