Skip to content

LazyColumn.str.pad

pad

pad(
    width: int,
    side: Literal["left", "right", "both"] = "left",
    fillchar: str = " ",
) -> LazyColumn

Pads each string in the column to the specified width with fillchar.

Parameters:

Name Type Description Default
width int

The total width of the resulting string after padding.

required
side Literal['left', 'right', 'both']

Which side(s) to pad. Options are "left", "right", or "both". Defaults to "left".

'left'
fillchar str

The character to use for padding. Defaults to a space.

' '

Returns:

Name Type Description
LazyColumn LazyColumn

A new LazyColumn with the strings padded to the given width.

Raises:

Type Description
ValueError

If side is not one of 'left', 'right', or 'both'.

NotImplementedError

If `side='both' is used, since it's not supported yet.

Examples:

print(df.head())
#    col1 my_string_column
# 0     1         "abc"
# 1     2         "1234"
# 2     3         None
# 3     4        "hello"
# 4     5         "x"

# Padding on the left to a width of 5 using '*'
df["my_string_column"].str.pad(5, side="left", fillchar="*")
# ["**abc", "*1234", None, "*hello", "****x"]

# Padding on the right to a width of 5 using '-'
df["my_string_column"].str.pad(5, side="right", fillchar="-")
# ["abc--", "1234-", None, "hello", "x----"]