pyspark.sql.functions.length

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

Computes the character length of string data or number of bytes of binary data. The length of character data includes the trailing spaces. The length of binary data includes binary zeros.

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

length of the value.

Examples

>>> spark.createDataFrame([('ABC ',)], ['a']).select(length('a').alias('length')).collect()
[Row(length=4)]