pyspark.sql.functions.trim

pyspark.sql.functions.trim(col: ColumnOrName) → pyspark.sql.column.Column[source]

Trim the spaces from both ends for the specified string column.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

target column to work on.

Returns
Column

trimmed values from both sides.

Examples

>>> df = spark.createDataFrame(["   Spark", "Spark  ", " Spark"], "STRING")
>>> df.select(trim("value").alias("r")).withColumn("length", length("r")).show()
+-----+------+
|    r|length|
+-----+------+
|Spark|     5|
|Spark|     5|
|Spark|     5|
+-----+------+