Convert IP address to integer and vice versa Last Updated : 01 Nov, 2020 Comments Improve Suggest changes Like Article Like Report We will use the ipaddress module for this purpose. ipaddress is a module that helps in the creation, manipulation and operation on IPv4 and IPv6 addresses and networks. The motivation of converting IP Addresses to integers and vice versa is that other modules that use IP addresses (such as socket) usually won’t accept objects from ipaddress module directly. Instead, they must be converted to string or an integer that the other module will accept. Syntax: ipaddress.ip_address(address) Parameter: Passing IP address in form of integer or string. Integers value less than 2**32 are considered as IPv4 addresses. Returns: IPv4Address or IPv6Address object is returned depending on the IP address passed as argument. If address passed does not represent a valid IPv4 or IPv6 address, a ValueError is raised. Program to convert integers to IP Address : Python3 # importing the module import ipaddress # converting int to IPv4 address print(ipaddress.ip_address(3221225000)) print(ipaddress.ip_address(123)) # converting int to IPv6 address print(ipaddress.ip_address(42540766400282592856903984001653826561)) Output: 191.255.254.40 0.0.0.123 2001:db7:dc75:365:220a:7c84:d796:6401 For converting IP addresses to integers: Python3 # importing the module import ipaddress # converting IPv4 address to int addr1 = ipaddress.ip_address('191.255.254.40') addr2 = ipaddress.ip_address('0.0.0.123') print(int(addr1)) print(int(addr2)) # converting IPv6 address to int addr3 = ipaddress.ip_address('2001:db7:dc75:365:220a:7c84:d796:6401') print(int(addr3)) Output: 3221225000 123 42540766400282592856903984001653826561 Comment More infoAdvertise with us Next Article Python | C++ | Remove leading zeros from an IP address M MuskanKalra1 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 python-utility python-modules +1 More Practice Tags : python Similar Reads Python | C++ | Remove leading zeros from an IP address Given an IP address, remove leading zeros from the IP address. Examples: Input : 100.020.003.400 Output : 100.20.3.400 Input :001.200.001.004 Output : 1.200.1.4Recommended PracticeRemove leading zeros from an IP addressTry It! The approach is to split the given string by â.â and then convert it to a 4 min read Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver 3 min read Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver 3 min read Convert String to Int in Python In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver 3 min read String to Int and Int to String in Python Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have 2 min read Python bit functions on int (bit_length, to_bytes and from_bytes) The int type implements the numbers.Integral abstract base class. 1. int.bit_length() Returns the number of bits required to represent an integer in binary, excluding the sign and leading zeros. Code to demonstrate Python3 1== num = 7 print(num.bit_length()) num = -7 print(num.bit_length()) Output: 1 min read Like