Python Program to Replace all Occurrences of ‘a’ with $ in a String
Last Updated :
18 Apr, 2023
Given a string, the task is to write a Python program to replace all occurrence of 'a' with $.
Examples:
Input: Ali has all aces
Output: $li h$s $ll $ces
Input: All Exams are over
Output: $ll Ex$ms $re Over
Method 1: uses splitting of the given specified string into a set of characters. An empty string variable is used to store modified string . We loop over the character array and check if the character at this index is equivalent to 'a' , and then append '$' sign, in case the condition is satisfied. Otherwise, the original character is copied into the new string.
Python3
# declaring a string variable
str = "Amdani athani kharcha rupaiya."
# declaring an empty string variable for storing modified string
modified_str = ''
# iterating over the string
for char in range(0, len(str)):
# checking if the character at char index is equivalent to 'a'
if(str[char] == 'a' or str[char] == 'a'.upper()):
# append $ to modified string
modified_str += '$'
else:
# append original string character
modified_str += str[char]
print("Modified string : ")
print(modified_str)
Output:
Modified string :
$md$ni $th$ni kh$rch$ rup$iy$.
Time Complexity: O(n), where n is length of str string.
Auxiliary Space: O(n), where n is length of modified_str string to store result.
Method 2: uses the inbuilt method replace() to replace all the occurrences of a particular character in the string with the new specified character. The method has the following syntax :
replace( oldchar , newchar)
This method doesn't change the original string, and the result has to be explicitly stored in the String variable.
Python3
# declaring a string variable
str = "An apple A day keeps doctor Away."
# replacing character a with $ sign
str = str.replace('a', '$')
str = str.replace('a'.upper(),'$')
print("Modified string : ")
print(str)
OutputModified string :
$n $pple $ d$y keeps doctor $w$y.
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using Python re module
The re.sub() function from the re module is used to replace all occurrences of a particular pattern in a string with a specified replacement. In this case, we are using the pattern 'a' and the replacement '$' to replace all occurrences of 'a' in the string str. The result of the re.sub() function is stored in the variable modified_str.
Python3
import re
#declaring a string variable
str = "Amdani athani kharcha rupaiya."
#using re.sub() function to replace all occurrences of 'a' with '$'
modified_str = re.sub("a", "$", str.lower())
#print("Modified string : ")
print(modified_str)
Output$md$ni $th$ni kh$rch$ rup$iy$.
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using list comprehension:
This approach uses a list comprehension to iterate over each character in the lowercase version of the input string str. If the character is equal to 'a', it is replaced with a '$' symbol. Otherwise, the original character is used. The resulting list of characters is then joined back into a string using the join() method.
Python3
# declaring a string variable
str = "Amdani athani kharcha rupaiya."
# using list comprehension to replace all occurrences of 'a' with '$'
modified_str = ''.join(['$' if c == 'a' else c for c in str.lower()])
# print modified string
print(modified_str)
Output$md$ni $th$ni kh$rch$ rup$iy$.
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n).
Similar Reads
Python | Replace multiple occurrence of character by single Given a string and a character, write a Python program to replace multiple occurrences of the given character by a single character. Examples: Input : Geeksforgeeks, ch = 'e' Output : Geksforgeks Input : Wiiiin, ch = 'i' Output : WinReplace multiple occurrence of character by singleApproach #1 : Nai
4 min read
Get Second Occurrence of Substring in Python String We are given a string and a substring, and our task is to find the index of the second occurrence of that substring within the string. This means we need to identify not just if the substring exists, but where it appears for the second time. For example, if we have a string like "hello world, hello
2 min read
Python program to convert camel case string to snake case Camel case is a writing style in which multiple words are concatenated into a single string, Snake case is another writing style where words are separated by underscores (_) and all letters are lowercase. We are going to cover how to convert camel case into snake case.Using Regular Expressions (re.s
3 min read
Python program to Replace all Characters of a List Except the given character Given a List. The task is to replace all the characters of the list with N except the given character. Input : test_list = ['G', 'F', 'G', 'I', 'S', 'B', 'E', 'S', 'T'], repl_chr = '*', ret_chr = 'G' Output : ['G', '*', 'G', '*', '*', '*', '*', '*', '*'] Explanation : All characters except G replace
4 min read
Python - Replace vowels in a string with a specific character K The task is to replace all the vowels (a, e, i, o, u) in a given string with the character 'K'. A vowel can be in uppercase or lowercase, so it's essential to account for both cases when replacing characters. Using str.replace() in a LoopUsing str.replace() in a loop allows us to iteratively replace
3 min read
Python Regex - Program to accept string starting with vowel Prerequisite: Regular expression in PythonGiven a string, write a Python program to check whether the given string is starting with Vowel or Not.Examples: Input: animal Output: Accepted Input: zebra Output: Not Accepted In this program, we are using search() method of re module.re.search() : This me
4 min read
Python program for removing i-th character from a string In this article, we will explore different methods for removing the i-th character from a string in Python. The simplest method involves using string slicing.Using String SlicingString slicing allows us to create a substring by specifying the start and end index. Here, we use two slices to exclude t
2 min read
Python Program to Accept the Strings Which Contains all Vowels The problem is to determine if a string contains all the vowels: a, e, i, o, u. In this article, weâll look at different ways to solve this.Using all()all() function checks if all vowels are present in the string. It returns True if every condition in the list comprehension is met.Pythons = "Geeksfo
2 min read
Python - Remove all consonants from string Sometimes, while working with Python, we can have a problem in which we wish to remove all the non vowels from strings. This is quite popular question and solution to it is useful in competitive programming and day-day programming. Lets discuss certain ways in which this task can be performed.Method
7 min read
Replace substring in list of strings - Python We are given a list of strings, and our task is to replace a specific substring within each string with a new substring. This is useful when modifying text data in bulk. For example, given a = ["hello world", "world of code", "worldwide"], replacing "world" with "universe" should result in ["hello u
3 min read