> For the complete documentation index, see [llms.txt](https://til.devjugal.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://til.devjugal.com/python/validate-ip-address.md).

# Validate IP Address

In order to validate an IP Address in Python, we can use `ipaddress` module. Examples below -

## Check if IP Address is valid

```python
>>> from ipaddress import ip_address
>>> ip_address("1.1.1.1")
IPv4Address('1.1.1.1')
>>> ip_address("::1")
IPv6Address('::1')
```

`ip_address` function throws an exception if the address is not a valid IPv4/IPv6 address.

## Check IP Address version

```python
>>> from ipaddress import ip_address
>>> ip_address("1.1.1.1").version
4
>>> ip_address("::1").version
6
```

We can also check the IP address version, by using `version` attribute on `ip_address` function.

***Source:*** [***StackOverFlow***](https://stackoverflow.com/a/10782565)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://til.devjugal.com/python/validate-ip-address.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
