# String Manipulation in PySpark!

In the world of data processing and analysis, data cleanliness is paramount. That's where PySpark's trim, ltrim, and rtrim functions come into play! They're your trusty allies for tidying up strings in DataFrames.

```python
from pyspark.sql import SparkSession

spark = SparkSession.builder.appName("SparkDemoApp").getOrCreate()
data = [(" Java ",), (" Scala ",), (" Python ",)]
df = spark.createDataFrame(data, ["languages"])
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694921147680/b357b40b-a9c5-4a61-ac8c-8f7ff86f4c4c.png align="left")

# Using trim()

Trim leading and trailing spaces

```python
from pyspark.sql.functions import trim, col
df = df.withColumn("cleaned_data", trim(col("languages")))
df.show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694921320735/e91529f4-7c4c-4582-b806-21e626367385.png align="center")

# Using .ltrim()

Trim leading spaces

```python
from pyspark.sql.functions import ltrim, col
df = df.withColumn("cleaned_data", ltrim(col("languages")))
df.show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694921463598/176ff29a-3c07-4e7d-9256-511bb71ed723.png align="center")

# Using .rtrim()

Trim white spaces at the end

```python
df = df.withColumn("cleaned_data", rtrim(col("languages")))
df.show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694921555990/c0355cee-df3e-4b33-ab3c-1aabb1f65887.png align="center")

> Do you want to connect with me I have started mentoring for career and interviews at [𝐭𝐨𝐩𝐦𝐚𝐭𝐞.𝐢𝐨/𝐧𝐚𝐯𝐞𝐞𝐧𝐩𝐧](https://topmate.io/naveenpn)
