---
title: ip_as_string
description: "ip\\_as\\_string function in PySpark: Returns the canonical string representation of an IP address or CIDR block."
canonical_url: https://docs.databricks.com/aws/en/pyspark/reference/functions/ip_as_string
---

# ip\_as\_string

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 18.2 and above

> [!NOTE]
> **Beta**
>
> This feature is in [Beta](https://docs.databricks.com/aws/en/release-notes/release-types.md).

Returns the canonical string representation of an IP address or CIDR block.

For the corresponding SQL function, see [`ip_as_string` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/ip_as_string.md).

## Syntax

```python
from pyspark.databricks.sql import functions as dbf

dbf.ip_as_string(col=<col>)
```

## Parameters

| Parameter | Type                          | Description                                                                       |
| --------- | ----------------------------- | --------------------------------------------------------------------------------- |
| `col`     | `pyspark.sql.Column` or `str` | A STRING or BINARY value representing a valid IPv4 or IPv6 address or CIDR block. |

## Examples

**Example 1**: Convert an IPv4 address to string.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('192.168.1.1',)], ['ip'])
df.select(dbf.ip_as_string('ip').alias('result')).collect()
```

```Output
[Row(result='192.168.1.1')]
```

**Example 2**: Convert an IPv6 address to string.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('2001:db8::1',)], ['ip'])
df.select(dbf.ip_as_string('ip').alias('result')).collect()
```

```Output
[Row(result='2001:db8::1')]
```

**Example 3**: Convert a CIDR block to string.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('192.168.1.5/24',)], ['cidr'])
df.select(dbf.ip_as_string('cidr').alias('result')).collect()
```

```Output
[Row(result='192.168.1.0/24')]
```

**Example 4**: `None` input returns `None`.

```python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([(None,)], 'ip: string')
df.select(dbf.ip_as_string('ip').alias('result')).collect()
```

```Output
[Row(result=None)]
```
