
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Defanging an IP Address in Python
Suppose we have a valid IPv4 IP address. We have to return the Defanged version of the IP address. A Defanged IP address is basically replace every period “.” by “[.]” So if the IP address is “192.168.4.1”, the output will be “192[.]168[.]4[.]1”
To solve this, we will follow these steps −
- We will split the string using dot, then put each element separated by “[.]”
Example
Let us see the following implementation to get better understanding −
class Solution(object): def defangIPaddr(self, address): address = address.split(".") return "[.]".join(address) ob1 = Solution() print(ob1.defangIPaddr("192.168.4.1"))
Input
"192.168.4.1"
Output
"192[.]168[.]4[.]1"
- Related Articles
- Validate IP Address in Python
- Python program to remove leading zeros from an IP address
- How to hide an IP address
- Difference between Static IP Address and Dynamic IP Address
- C Program to validate an IP address
- Python program to remove leading 0's from an IP address
- Validate IP Address in C#
- Validate IP Address in C++
- Python program to Display Hostname and IP address?
- Difference between IP Address and MAC Address
- Difference between MAC Address and IP Address
- Alias/Secondary IP Address
- Python program to find the IP Address of the client
- Which MySQL datatype to used to store an IP address?
- Determining the User IP Address in Node

Advertisements