![]() |
VOOZH | about |
The replace() method returns a new string where all occurrences of a specified substring are replaced with another substring. It does not modify the original string because Python strings are immutable.
Example : This example replaces every occurrence of a substring in the given string, creating a fully updated new string.
Coding is fun. Coding is powerful.
string.replace(old, new, count)
Parameters:
Return Value: Returns a new string with the specified replacements. The original string remains unchanged.
Example 2: Here, only the first occurrence of a substring is replaced using the count parameter.
orange apple apple
Explanation: s.replace("apple", "orange", 1) replaces "apple" only once due to count=1.
Example 3: This example demonstrates that replace() treats uppercase and lowercase characters as different, replacing only exact matches.
Hi World! hello world! Hello World! hi world!
Explanation: