Change language

Python | Remove leading zeros from IP address

| |

Examples :

 Input: 100.020.003.400 Output: 100.20.3.400 Input: 001.200.001.004 Output: 1.200.1.4 

Method 1: Bypass and join

The approach is to split the given string by "." and then convert it to integer a number that removes leading zeros and then concatenate them back into a string. To convert a string to an integer, we can use

# Python program to remove leading zeros
# IP address and print IP

  
# function to remove leading zeros

def removeZeros (ip):

 

# splits ip into & quot;. & quot;

  # convert Assigns words to integers to remove leading ones removeZeros

# convert back integer to string and concatenate them back to the line

new_ip = "." . join ([ str ( int (i)) for i in ip.split ( ". " )]) 

  return new_ip; 

 

 
# driver code
# example1

ip = "100.020.003.400"  

print (removeZeros (ip))

 

 
# example2

ip = "001.200.001.004"

print (removeZeros (ip))

Output:

 100.20.3.400 1.200.1.4 

Method 2: regex

And When using a capture group, match the last digit and copy it to prevent all digits from being replaced.

d Matches any decimal digit, this is equivalent to the set class [0-9].

  • / b allows you to perform word-only searches using a regular expression like / bword / b. 
    the regular expression / b can be explained as:
      allows you to perform a "whole words only" search u sing a regular expression in the form of word  
  • # Python program to remove leading zeros
    # IP address and print IP using regular expressions

    import re 

     
    # function to remove leading zeros

    def removeZeros (ip):

    new_ip = re.sub (r ’0+ (d) ’ , r ’ 1’ , ip)

    # splits ip into & quot;. & quot;

    # converts words to integers to remove leading removeZeros

    # convert back an integer to a string and concatenate them back to a string

     

    return new_ip 

     

     
    # driver code
    # example1

    ip = "100.020.003.400"  

    print (removeZeros (ip))

      

      
    # example2

    ip = "001.200.001.004"

    print (removeZeros (ip))

    Exit:

     100.20.3.400 1.200.1.4 

    Shop

    Learn programming in R: courses

    $

    Best Python online courses for 2022

    $

    Best laptop for Fortnite

    $

    Best laptop for Excel

    $

    Best laptop for Solidworks

    $

    Best laptop for Roblox

    $

    Best computer for crypto mining

    $

    Best laptop for Sims 4

    $

    Latest questions

    NUMPYNUMPY

    Common xlabel/ylabel for matplotlib subplots

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    12 answers

    NUMPYNUMPY

    Flake8: Ignore specific warning for entire file

    12 answers

    NUMPYNUMPY

    glob exclude pattern

    12 answers

    NUMPYNUMPY

    How to avoid HTTP error 429 (Too Many Requests) python

    12 answers

    NUMPYNUMPY

    Python CSV error: line contains NULL byte

    12 answers

    NUMPYNUMPY

    csv.Error: iterator should return strings, not bytes

    12 answers

    News


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    sin

    How to specify multiple return types using type-hints

    exp

    Printing words vertically in Python

    exp

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    cos

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically