pyspark.sql.functions.rpad#

pyspark.sql.functions.rpad(col, len, pad)[source]#

Right-pad the string column to width len with pad.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

target column to work on.

lenColumn or int

length of the final string.

Changed in version 4.0.0: pattern now accepts column.

padColumn or literal string

chars to prepend.

Changed in version 4.0.0: pattern now accepts column.

Returns
Column

right padded result.

Examples

Example 1: Pad with a literal string

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([('abcd',), ('xyz',), ('12',)], ['s',])
>>> df.select("*", sf.rpad(df.s, 6, '#')).show()
+----+-------------+
|   s|rpad(s, 6, #)|
+----+-------------+
|abcd|       abcd##|
| xyz|       xyz###|
|  12|       12####|
+----+-------------+

Example 2: Pad with a bytes column

>>> from pyspark.sql import functions as sf
>>> df = spark.createDataFrame([('abcd',), ('xyz',), ('12',)], ['s',])
>>> df.select("*", sf.rpad(df.s, 6, sf.lit(b"uv"))).show()
+----+-------------------+
|   s|rpad(s, 6, X'7576')|
+----+-------------------+
|abcd|             abcduv|
| xyz|             xyzuvu|
|  12|             12uvuv|
+----+-------------------+