Python program to check if the given string is IPv4 or IPv6 or Invalid Last Updated : 28 Jul, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string. The task is to check if the given string is IPv4 or IPv6 or Invalid. Examples: Input : "192.168.0.1" 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. Python3 from ipaddress import ip_address, IPv4Address def validIPAddress(IP: str) -> str: try: return "IPv4" if type(ip_address(IP)) is IPv4Address else "IPv6" except ValueError: return "Invalid" if __name__ == '__main__' : # Enter the Ip address Ip = "192.168.0.1" print(validIPAddress(Ip)) Ip = "2001:0db8:85a3:0000:0000:8a2e:0370:7334" print(validIPAddress(Ip)) Ip = "256.32.555.5" print(validIPAddress(Ip)) Ip = "250.32:555.5" print(validIPAddress(Ip)) Output : IPv4 IPv6 Invalid Invalid Comment More infoAdvertise with us Next Article Python program to find if two IP Address belongs to Same or Different Network C chirags_30 Follow Improve Article Tags : Python Python Programs python-utility Python string-programs Practice Tags : python Similar Reads Python program to determine if the given IPv4 Address is reserved using ipaddress module Given a IPv4 Address, the task is to determine whether it is reserved (i.e belongs to class E) or not. What is class E? IP addresses belonging to class E are reserved for experimental and research purposes. IP addresses of class E range from 240.0.0.0 â 255.255.255.254. This class doesnât have any s 1 min read Python Program to test if the String only Numbers and Alphabets Given a String, our task is to write a Python program to check if string contains both numbers and alphabets, not either nor punctuations. Examples: Input : test_str = 'Geeks4Geeks' Output : True Explanation : Contains both number and alphabets. Input : test_str = 'GeeksforGeeks' Output : False Expl 4 min read Python program to find if two IP Address belongs to Same or Different Network Prerequisites: Classless Inter Domain Routing (CIDR) Given two IP Addresses in CIDR Notation determine whether they belong to Same Network or Different Network. Two IP addresses are said to be in Same Network if the Network ID of both the IP Addresses are same. Examples: Input : IP1 = 192.168.1.0/8, 2 min read Python - Test if Kth character is digit in String Given a String, check if Kth index is a digit. Input : test_str = 'geeks9geeks', K = 5 Output : True Explanation : 5th idx element is 9, a digit, hence True.Input : test_str = 'geeks9geeks', K = 4 Output : False Explanation : 4th idx element is s, not a digit, hence False. Method #1: Using in operat 5 min read Python program to find the type of IP Address using Regex Prerequisite: Python Regex Given an IP address as input, write a Python program to find the type of IP address i.e. either IPv4 or IPv6. If the given is neither of them then print neither. What is an IP (Internet Protocol) Address?Every computer connected to the Internet is identified by a unique fo 2 min read Check Whether String Contains Only Numbers or Not - Python We are given a string s="12345" we need to check whether the string contains only number or not if the string contains only number we will return True or if the string does contains some other value then we will return False. This article will explore various techniques to check if a string contains 3 min read Like