pyspark.sql.functions.substring#
- pyspark.sql.functions.substring(str, pos, len)[source]#
Substring starts at pos and is of length len when str is String type or returns the slice of byte array that starts at pos in byte and is of length len when str is Binary type.
New in version 1.5.0.
Changed in version 3.4.0: Supports Spark Connect.
- Parameters
- str
Columnor str target column to work on.
- posint
starting position in str.
- lenint
length of chars.
- str
- Returns
Columnsubstring of given value.
Notes
The position is not zero based, but 1 based index.
Examples
>>> df = spark.createDataFrame([('abcd',)], ['s',]) >>> df.select(substring(df.s, 1, 2).alias('s')).collect() [Row(s='ab')]