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

    psycopg2: insert multiple rows with one query

    12 answers

    NUMPYNUMPY

    How to convert Nonetype to int or string?

    12 answers

    NUMPYNUMPY

    How to specify multiple return types using type-hints

    12 answers

    NUMPYNUMPY

    Javascript Error: IPython is not defined in JupyterLab

    12 answers

    News


    Wiki

    Python OpenCV | cv2.putText () method

    numpy.arctan2 () in Python

    Python | os.path.realpath () method

    Python OpenCV | cv2.circle () method

    Python OpenCV cv2.cvtColor () method

    Python - Move item to the end of the list

    time.perf_counter () function in Python

    Check if one list is a subset of another in Python

    Python os.path.join () method