Skip to content
Related Articles

Related Articles

Python program to check if the given string is IPv4 or IPv6 or Invalid
  • Difficulty Level : Easy
  • Last Updated : 28 Jul, 2020

Given a string. The task is to check if the given string is IPv4 or IPv6 or Invalid.


Input : “”
Output : IPv4
Explanation : It is a valid IPv4 address

Input : “2001:0db8:85a3:0000:0000:8a2e:0370:7334”
Output : IPv6
Explanation : It is a valid IPv6 address

Input : “255.32.555.5”
Output : Invalid
Explanation : It is an invalid IPv4 address as the 3rd octet value(i.e 555) is greater 255.

Input : “250.32:555.5”
Output : Invalid
Explanation : The given string is invalid as it consists of both : and .

To implement the above problem, we will use the ipaddress module in Python. This module provides the capabilities to create, manipulate, and operate on IPv4 and IPv6 addresses and networks.

Below is the implementation.

from ipaddress import ip_address, IPv4Address
def validIPAddress(IP: str) -> str:
        return "IPv4" if type(ip_address(IP)) is IPv4Address else "IPv6"
    except ValueError:
        return "Invalid"
if __name__ == '__main__' :  
    # Enter the Ip address 
    Ip = ""
    Ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
    Ip = "256.32.555.5"
    Ip = "250.32:555.5"

Output :


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :