This article discusses three methods that can be used to replace commas with dots in a Python string. These methods include:
- Using str.replace() function,
- Using str.maketrans and str.translate(), and
- Using a find-and-replace strategy in a loop
The code examples in this article can be executed in Python 2 or Python 3.
Method 1: Using str.replace() function
The function str.replace(substring1, substring2) returns a copy of str will all occurrences of substring1 are replaced by substring2.
In this method, we need a third character (not a dot or a comma) that will be used to replace the first character (dot or comma) at the start to ensure that there’s no conflict when replacing. The third character should not be present in the string we are processing.
Example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def SwapReplace(str1): """ Takes the string and swaps dots with commas. Returns a string with the characters of interest swapped. """ # Replace dot (.) with a dollar sign ($)(you can use any other # character not in the input string) str1 = str1.replace(".", "$") # Replace comma (,) with dot (.) - a needed swap str1 = str1.replace(",", ".") # Replace the dollar sign ($) with a comma (,) - a needed swap. translated_str1 = str1.replace("$", ",") return translated_str1 # Calling SwapReplace() function translated_string = SwapReplace('10.145,68') print(translated_string) |
Output:
10,145.68
Method 2: Using str.maketrans and str.translate()
The str.maketrans is a static method that returns a mapping/translation table that can be used with the str.translate() function to replace specified characters.
1 2 3 4 5 6 7 8 9 10 11 |
try: from string import maketrans # For Python 2 except ImportError: maketrans = str.maketrans # For Python 3 def SwapTrans(str1): translated_str1 = str1.translate(maketrans(".,", ",.")) return translated_str1 translated_str = SwapTrans("10.145,68") print(translated_str) |
Output:
10,145.68
Method 3: Using a dictionary find-and-replace strategy
In this method, we need to understand the use of dict.get(key[, default]) method. The function returns the value for the key if the key is in the dictionary, else default. Example,
1 2 3 4 5 6 7 8 9 |
dict1 = { "name": "smith", "address": "23" } # name key exists in the dictionary therefore # its value is returned print(dict1.get("name", "Bob")) # location key is not in the dictionary # therefore, the default value is returned. print(dict1.get("location", "99")) |
Output:
smith 99
Once that is clear, we can work on an example of swapping commas with dots.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
swap_dict = {'.':',',' , ':'.'} def SwapCommasAndDots(str1): """ Argument: string Working: Iterate through the string characters. If the character being checked is not a comma (,) or dot (.) the default (the character itself) is returned else; values are replaced based on swap_dict. Returns: translated_str1 """ translated_str1= ''.join(swap_dict.get(i, i) for i in str1) return translated_str1 translated_string = SwapCommasAndDots("10.145,68") print(translated_string) |
Output:
10,145.68