pyspark.sql.functions.substr#

pyspark.sql.functions.substr(str, pos, len=None)[source]#

Returns the substring of str that starts at pos and is of length len, or the slice of byte array that starts at pos and is of length len.

Added in version 3.5.0.

Parameters:
strColumn or column name

A column of string. A column that evaluates to a string or binary.

posColumn or column name

The starting position of the substring. A column that evaluates to an integer.

lenColumn or column name, optional

The length of the substring. A column that evaluates to an integer.

Returns:
Column

substring of given value. Returns a column of the same type as the input.

Examples

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([("Spark SQL", 5, 1,)], ["a", "b", "c"])
>>> df.select("*", sf.substr("a", "b", "c")).show()
+---------+---+---+---------------+
|        a|  b|  c|substr(a, b, c)|
+---------+---+---+---------------+
|Spark SQL|  5|  1|              k|
+---------+---+---+---------------+
>>> df.select("*", sf.substr(df.a, df.b)).show()
+---------+---+---+------------------------+
|        a|  b|  c|substr(a, b, 2147483647)|
+---------+---+---+------------------------+
|Spark SQL|  5|  1|                   k SQL|
+---------+---+---+------------------------+