Write a Python program to swap comma and dot in a string


This program is swapping the comma and dot in a string. The string "23,2000.00" is assigned to the variable "val". The original string is then printed to the console using the "print" function with the message "Before Swap Comma and Dot : ".

Next, the "maketrans" function is used to create a translation table which will be used to translate the characters in the string. The translation table is constructed using the "maketrans" method with the arguments ",'." and '.,'. The first argument represents the characters to be replaced and the second argument represents the characters to be replaced with.

Finally, the "translate" method is used to translate the characters in the string using the translation table created above. The result of this translation is then assigned back to the "val" variable. The final result is then printed to the console using the "print" function with the message "After Swap Comma and Dot : ".

Source Code

val = "23,2000.00"
print("Before Swap Comma and Dot : ",val)
maketrans = val.maketrans
val = val.translate(maketrans(',.', '.,'))
print("After Swap Comma and Dot : ",val)

Output

Before Swap Comma and Dot :  23,2000.00
After Swap Comma and Dot :  23.2000,00

Example Programs