The problem is quite simple. Given a string, we need to replace all commas with dots and all dots with the commas. This can be achieved in two popular ways.
Input : 14, 625, 498.002 Output : 14.625.498, 002
Using maketrans and translate()
maketrans: This static method returns a translation table usable for str.translate(). This builds a translation table, which is a mapping of integers or characters to integers, strings, or None.
translate: This returns a copy of the string where all characters occurring in the optional argument are removed, and the remaining characters have been mapped through the translation table, given by the maketrans table.
For more reference visit Python String Methods.
This is more of a logical approach in which we swap the symbols considering third variables. The replace method can also be used to replace the methods in strings. We can convert “, ” to a symbol then convert “.” to “, ” and the symbol to “.”. For more reference visit Python String Methods.
- Print number with commas as 1000 separators in Python
- Python Bokeh - Plotting Dots on a Graph
- Python Bokeh - Plotting Diamond Dots on a Graph
- Python Bokeh - Plotting Hexagon Dots on a Graph
- Python Bokeh - Plotting Triangles with Dots on a Graph
- Python Bokeh - Plotting Squares with Dots on a Graph
- What is Three dots(...) or Ellipsis in Python3
- Python - Swap elements in String list
- Python program to Swap Keys and Values in Dictionary
- Python | Swap Name and Date using Group Capturing in Regex
- Python - Swap ith and jth key's value in dictionary
- Python program to swap two elements in a list
- Python | Swap tuple elements in list of tuples
- Python Program to Swap Two Variables
- Python Program to swap two numbers without using third variable
- Python - Swap K suffix with prefix
- How to swap columns of a given NumPy array?
- Swap two variables in one line
- String to Int and Int to String in Python
- String slicing in Python to check if a string can become empty by recursive deletion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.