GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python String replace() Method - GeeksforGeeks Python String replace() MethodLast Updated : 7 Jul, 2026 replace() method is used to replace a specified substring with another substring in a string. It returns a new string with the replacements applied, while the original string remains unchanged.
Output Coding is fun. Coding is powerful. Explanation:
Syntax
Parameters:
Return Value: A new string with the specified replacements applied. The original string is not modified because strings in Python are immutable. ExamplesExample 1: Here, only the first occurrence of a substring is replaced using the count parameter.
Output orange apple apple Explanation: s.replace("apple", "orange", 1) replaces "apple" only once due to count=1. Example 2: This example demonstrates that replace() treats uppercase and lowercase characters as different, replacing only exact matches.
Output Hi World! hello world! Hello World! hi world! Explanation:
Example 3: In this example, replace() is used multiple times to clean and reformat a phone number.
Output 123 456 7890 Explanation: replace("-", " ") replaces each hyphen (-) with a space. Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |