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 ) |
Modified 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) |
$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) |
$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).