- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Replace all Occurrences of ‘a’ with $ in a String
When it is required to replace all the occurrences of ‘a’ with a character such as ‘$’ in a string, the string can be iterated over and can be replaced using the ‘+=’ operator.
Below is a demonstration of the same −
Example
my_str = "Jane Will Rob Harry Fanch Dave Nancy" changed_str = '' for char in range(0, len(my_str)): if(my_str[char] == 'a'): changed_str += '$' else: changed_str += my_str[char] print("The original string is :") print(my_str) print("The modified string is : ") print(changed_str)
Output
The original string is : Jane Will Rob Harry Fanch Dave Nancy The modified string is : J$ne Will Rob H$rry F$nch D$ve N$ncy
Explanation
A string is defined and is displayed on the console.
An empty string is also defined.
The string is iterated, and if the alphabet ‘a’ is encountered, it is replaced with ‘$’.
Otherwise, it is not changed.
The resultant output is displayed on the console.
- Related Articles
- How to replace all occurrences of a string with another string in Python?
- Replace All Occurrences of a Python Substring with a New String?
- Java Program to replace all occurrences of a given character in a string
- Java Program to replace all occurrences of given String with new one
- How to replace all occurrences of a string in JavaScript?
- How to replace all occurrences of a word in a string with another word in java?
- Java Program to replace all occurrences of a given character with new character
- C++ program to replace all occurrences of string AB with C without using extra space
- C# program to replace all spaces in a string with ‘%20’
- How can we replace all the occurrences of a substring with another substring within a string in MySQL?
- Java Program to replace only first occurrences of given String with new one
- Python program to count occurrences of a word in a string
- C program to replace all occurrence of a character in a string
- Write a python program to count occurrences of a word in string?
- Replace All Consonants with Nearest Vowels In a String using C++ program

Advertisements