StringMethods.slice_replace(start: int | None = None, stop: int | None = None, repl: str | None = None) SeriesOrIndex#

Replace the specified section of each string with a new string.

startint, optional

Beginning position of the string to replace. Default is beginning of the each string.

stopint, optional

Ending position of the string to replace. Default is end of each string.

replstr, optional

String to insert into the specified position values.

Series/Index of str dtype

A new string with the specified section of the string replaced with repl string.

See also


Just slicing without replacement.


>>> import cudf
>>> s = cudf.Series(['a', 'ab', 'abc', 'abdc', 'abcde'])
>>> s
0        a
1       ab
2      abc
3     abdc
4    abcde
dtype: object

Specify just start, meaning replace start until the end of the string with repl.

>>> s.str.slice_replace(1, repl='X')
0    aX
1    aX
2    aX
3    aX
4    aX
dtype: object

Specify just stop, meaning the start of the string to stop is replaced with repl, and the rest of the string is included.

>>> s.str.slice_replace(stop=2, repl='X')
0       X
1       X
2      Xc
3     Xdc
4    Xcde
dtype: object

Specify start and stop, meaning the slice from start to stop is replaced with repl. Everything before or after start and stop is included as is.

>>> s.str.slice_replace(start=1, stop=3, repl='X')
0      aX
1      aX
2      aX
3     aXc
4    aXde
dtype: object