Python program to swap commas and dots in a string Example
Simple example code to swap comma and dot in a string.
Using maketrans and translate()
It should swap commas and dots in the given string.
amount = "12.345,678" maketrans = amount.maketrans amount = amount.translate(maketrans(',.', '.,')) print(amount)
With replacing method can convert “,” comma to a symbol then convert “.” dot to “,” comma and the symbol to “.” dot.
amount = "12.345,678" def swap(str1): str1 = str1.replace(',', 'third') str1 = str1.replace('.', ', ') str1 = str1.replace('third', '.') return str1 print(swap(amount))
Output: 12, 345.678
Do comments if you have any doubts and suggestions on this Python swap code.
Note: IDE: PyCharm 2021.1.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.