Given an IP address as input, the task is to write a Python program to check whether the given IP Address is Valid or not without using RegEx.
What is an IP (Internet Protocol) Address?
Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) address. An IP address (version 4) consists of four numbers (each between 0 and 255) separated by periods. The format of an IP address is a 32-bit numeric address written as four decimal numbers (called octets) separated by periods; each number can be written as 0 to 255 (E.g. – 0.0.0.0 to 255.255.255.255).
Input: 192.168.0.1 Output: Valid Ip address Input: 126.96.36.199 Output: Valid Ip address Input: 6188.8.131.52 Output: Invalid Ip addess
Method #1: Checking the number of periods using count() method and then the range of numbers between each period.
Invalid Ip addess
Method #2: Using a variable to check the number of periods and using a set to check if the range of numbers between periods is between 0 and 255(inclusive).
Valid Ip address
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.