[英]How to split string before and after certain characters?
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?我有一个任务要创建一个 python 程序,它将接收一个 IP 地址并检查它是否有效,我的问题是我不知道如何拆分输入所以我将能够检查每个八位位组是否在 0 - 255 或不是?
You want to use the "split()" method.你想使用“split()”方法。
ip = "192.68.10.10"
ip_arr = ip.split(".")
print(ip_arr)
This will return an array containing the 4 numbers:这将返回一个包含 4 个数字的数组:
['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)然后您可以遍历数组以检查每个值是否小于 256。(不要忘记在检查之前转换为 int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.