Friday, October 14, 2016

Splitting IP Address string variable in Python

Python variable types post defines the different type’s variable in python. In this post, I will be covering the basis stuff which is used to convert a string into an integer and use it for further validation. The main intent of this article is to write few lines of code which can ask user to input the “IP Address Field” and split the string into different octets by using split function. Once this is done, after that convert the string to integer type so that we can check whether the given input has length of 4 or not (In case of IP Address we have a.b.c.d).

The same lines of codes can become more meaningful and useful when it is applied with if-else or with any kind of loop statements.
The given code is plain code which is used for basic understanding.
# raw_input is used whenever we want user to provide the inputs. Storing the user values in ip_address variable
ip_address = raw_input( "Enter an IP Address = ")

# Check the whether the entered IP Address is string or integer. Python has inbuilt function called type which is useful to check the variable type.
ip_address_check = type(ip_address)

print "Entered IP Address type is %s" %ip_address_check

print "\nNow we are convering this IP Address to integer\n"

print "Lets split the IP Address into four octet by using dot.\n"

# split function is used divide the string into smaller one with delimiter of dot. If user enters, in this case whenever DOT is seen, string is divided into four different parts by using split function ip_address_split = ip_address.split(".")

# By using split function, we can store the given user inputs into four arrays. Ist element is pointing towards first location which is 0.
print "The first octet is %s" %ip_address_split[0] #----> Output will be 192
print "The second octet is %s"%ip_address_split[1] #----> Output will be 168
print "The Third octet is %s"%ip_address_split[2] #----> Output will be 1
print "The Fourth octet is %s"%ip_address_split[3] #----> Output will be 1

print "\nLets check whether the length is equal to four or not - Octet Validation\n"

# len is length function which is used to check the length of string after spliting it using split function. We can use the conditional statements to check it. In this case we are not using anything.

ip_address_length = len(ip_address_split)

print "The length of IP Address is %d" %ip_address_length

print "\nCurrently everything is in string, lets convert this IP Address to integer\n"

# int is integer function which is used to convert the first string element into integer. Once this is done, we can simply compare it with different IP Addresses classes and check that this IP address is falling in which class. We are not using it here but it can be used by using conditional statements.

ip_address_split_convert = int(ip_address_split[0])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion ist octet, type is changed to integrer %s" %ip_address_split_convert_check

ip_address_split_convert = int(ip_address_split[1])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion second octet, type is changed to integrer %s" %ip_address_split_convert_check

ip_address_split_convert = int(ip_address_split[2])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion third octet, type is changed to integrer %s" %ip_address_split_convert_check

ip_address_split_convert = int(ip_address_split[3])
ip_address_split_convert_check = type(ip_address_split_convert)
print "After conversion fourth octet, type is changed to integrer %s" %ip_address_split_convert_check

People who read this post also read :

No comments: