Python: Valid an IP address
IP Address Validator
Write a Python program to validate an IP address.
From Wikipedia,
An Internet Protocol address (IP address) is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. An IP address serves two main functions: host or network interface identification and location addressing.
Internet Protocol version 4 (IPv4) defines an IP address as a 32-bit number. However, because of the growth of the Internet and the depletion of available IPv4 addresses, a new version of IP (IPv6), using 128 bits for the IP address, was standardized in 1998. IPv6 deployment has been ongoing since the mid-2000s.
IP addresses are written and displayed in human-readable notations, such as 172.16.254.1 in IPv4, and 2001:db8:0:1234:0:567:8:1 in IPv6. The size of the routing prefix of the address is designated in CIDR notation by suffixing the address with the number of significant bits, e.g., 192.168.1.15/24, which is equivalent to the historically used subnet mask 255.255.255.0.
Sample Solution-1:
Python Code:
Sample Output:
Invalid IP
Flowchart:

Sample Solution-2:
Python Code:
10.0.0.0 -> Valid Ip address 10.255.255.255 -> Valid Ip address 192.168.255.0 -> Valid Ip address 266.1.0.2 -> Invalid Ip address 01.102.103.104 -> Invalid Ip address
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to check if an IP address belongs to a specific subnet.
- Write a Python function to validate both IPv4 and IPv6 addresses.
- Write a Python program to extract all valid IP addresses from a string.
- Write a Python function to classify an IP address as public or private.
Go to:
Previous: Write a Python program to convert true to 1 and false to 0.
Next: Write a Python program to convert an integer to binary keep leading zeros.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.