pyspark.sql.functions.format_string#

pyspark.sql.functions.format_string(format, *cols)[source]#

Formats the arguments in printf-style and returns the result as a string column.

Added in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters:
formatliteral string

string that can contain embedded format tags and used as result column’s value. A column that evaluates to a string.

colsColumn or column name

column names or Columns to be used in formatting Each a column of any type.

Returns:
Column

the column of formatted results. Returns a column that evaluates to a string.

Examples

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([(5, "hello")], ["a", "b"])
>>> df.select("*", sf.format_string('%d %s', "a", df.b)).show()
+---+-----+--------------------------+
|  a|    b|format_string(%d %s, a, b)|
+---+-----+--------------------------+
|  5|hello|                   5 hello|
+---+-----+--------------------------+