Defanging an IP Address - Problem

Given a valid IPv4 IP address, you need to "defang" it for security purposes. In web security, displaying IP addresses directly can be a vulnerability, so we often need to make them safe for display.

A defanged IP address replaces every period "." with "[.]" to prevent it from being interpreted as a clickable link or executable code.

Example: The IP address "192.168.1.1" becomes "192[.]168[.]1[.]1"

Goal: Transform the input IP address string by replacing all dots with [.] and return the defanged version.

Input & Output

example_1.py โ€” Basic IP Address
$ Input: address = "1.1.1.1"
โ€บ Output: "1[.]1[.]1[.]1"
๐Ÿ’ก Note: Each dot in the IP address is replaced with [.] to create the defanged version
example_2.py โ€” Standard IP Address
$ Input: address = "255.100.50.0"
โ€บ Output: "255[.]100[.]50[.]0"
๐Ÿ’ก Note: All three dots are replaced with [.], keeping all numeric parts unchanged
example_3.py โ€” Common Private IP
$ Input: address = "192.168.1.1"
โ€บ Output: "192[.]168[.]1[.]1"
๐Ÿ’ก Note: This common private network IP address gets defanged by replacing all dots with [.]

Constraints

  • The given address is a valid IPv4 address
  • IPv4 addresses consist of 4 integers separated by dots
  • Each integer is between 0 and 255 inclusive
  • The input will always be a valid IP address format

Visualization

Tap to expand
IP Address Defanging Visualization๐Ÿšจ UNSAFE - Active IP Address:192.168.1.1โš™๏ธโœ… SAFE - Defanged IP Address:192[.]168[.]1[.]1Step 1:Find dotsStep 2:Replace with [.]Step 3:Keep numbersStep 4:Return safe
Understanding the Visualization
1
Identify Dots
Scan through the IP address to find all dot characters
2
Replace Safely
Replace each dot with [.] to neutralize its network meaning
3
Preserve Numbers
Keep all numeric parts exactly the same
4
Return Safe String
The result is a defanged IP that won't be interpreted as a network address
Key Takeaway
๐ŸŽฏ Key Insight: String replacement is about pattern matching and substitution - identify the target pattern (dots) and replace with the safe pattern ([.])
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 5
67.0K Views
High Frequency
~5 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen