SyntaxStudy
Sign Up

replace()

method

Returns a copy of the string with all (or count) occurrences of old replaced by new.

Syntax

str.replace(old, new, count=-1)

Example

python
'Hello World'.replace('World', 'Python')  # 'Hello Python'
'aabbcc'.replace('b', 'X')                # 'aaXXcc'
'aabbcc'.replace('b', 'X', 1)             # 'aaXbcc'

# Clean whitespace
'hello   world'.replace('   ', ' ')       # 'hello world'