Validate an IP address using Python without using RegEx
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: 18.104.22.168 Output: Valid Ip address Input: 622.214.171.124 Output: Invalid Ip address
Method #1: Checking the number of periods using count() method and then the range of numbers between each period.
Invalid Ip address
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course