How to Replace String Characters with Others in Python


In this example, we will learn the methods to replace characters in a string with other characters with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

str = input("Please input the string: ")
a = input("Please input the string to be replaced: ")
b = input("Please input the string to replace: ")
str = str.replace(a, b)
print("After replacing: ")
print(str)

Output:

Please input the string: banana
Please input the string to be replaced: a
Please input the string to replace: @
After replacing: 
b@n@n@
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments