I have an assignment to create a python program that will receive an IP address and check if its valid or not, my problem is I dont know how to split the input so I will be able to check each octet whether its between 0 - 255 or not ?
Asked
Active
Viewed 398 times
0
-
2You can use `string.split(".")` – Mateus Nov 12 '20 at 14:30
-
Welcome to Stackoverflow. Make sure to post some of your own attempts at solving the problem. Also, read documentation on ```string.split(separator, maxsplit)``` – Serge de Gosson de Varennes Nov 12 '20 at 15:38
2 Answers
0
You want to use the "split()" method.
ip = "192.68.10.10"
ip_arr = ip.split(".")
print(ip_arr)
This will return an array containing the 4 numbers:
['192', '68', '10', '10']
You can then loop through the array to check if each value is under 256. (Don't forget to convert to int before checking)

Shiverz
- 663
- 1
- 8
- 23