Introduction
Replacing a substring in a string is a common task in text processing. This operation involves searching for a specific substring within a string and replacing it with another substring. This tutorial will guide you through creating a Python program that replaces a substring in a given string.
Example:
-
Input:
hello world, replaceworldwithPython -
Output:
hello Python -
Input:
Python programming is fun, replacefunwithawesome -
Output:
Python programming is awesome
Problem Statement
Create a Python program that:
- Takes a string, a target substring, and a replacement substring as input.
- Replaces all occurrences of the target substring with the replacement substring.
- Displays the modified string.
Solution Steps
- Take Input from the User: Use the
input()function to get the original string, the target substring, and the replacement substring from the user. - Replace the Substring: Use the
replace()method to replace all occurrences of the target substring with the replacement substring. - Display the Modified String: Use the
print()function to display the modified string.
Python Program
# Python Program to Replace a Substring in a String
# Author: https://www.rameshfadatare.com/
# Step 1: Take input from the user
original_string = input("Enter the original string: ")
target_substring = input("Enter the substring to be replaced: ")
replacement_substring = input("Enter the replacement substring: ")
# Step 2: Replace the target substring with the replacement substring
modified_string = original_string.replace(target_substring, replacement_substring)
# Step 3: Display the modified string
print(f"The modified string is: {modified_string}")
Explanation
Step 1: Take Input from the User
- The
input()function prompts the user to enter the original string, the target substring, and the replacement substring. These inputs are stored in the variablesoriginal_string,target_substring, andreplacement_substring, respectively.
Step 2: Replace the Substring
- The
replace()method is used to search for all occurrences of the target substring in the original string and replace them with the replacement substring. The modified string is stored in the variablemodified_string.
Step 3: Display the Modified String
- The
print()function is used to display the modified string with the replacements made.
Output Example
Example 1:
Enter the original string: hello world
Enter the substring to be replaced: world
Enter the replacement substring: Python
The modified string is: hello Python
Example 2:
Enter the original string: Python programming is fun
Enter the substring to be replaced: fun
Enter the replacement substring: awesome
The modified string is: Python programming is awesome
Example 3:
Enter the original string: I love programming
Enter the substring to be replaced: programming
Enter the replacement substring: coding
The modified string is: I love coding
Conclusion
This Python program demonstrates how to replace a substring in a string using the replace() method. This is used in text processing and is often used in scenarios where text needs to be modified or updated dynamically. The example is straightforward and helps beginners understand string manipulation in Python.