Python - Swap commas and dots in a String Last Updated : 27 Dec, 2024 Summarize Suggest changes Share Like Article Like Report In this article, we'll explore how to swap commas and dots in a string in Python. Using str.translate() str.translate() that allows for character substitution within a string using a translation table. It enables quick transformations, such as swapping characters like commas and dots.Example: Python # Create a translation table # to map commas to dots and vice versa t = str.maketrans(',.', '.,') s = "14, 625, 498.002" # Swap commas and dots res = s.translate(t) print(res) Output14. 625. 498,002 Let's understand different methods to swap commas and dots in a string.Table of ContentUsing str.replace()Using regular expression Using List comprehensionUsing for LoopUsing str.replace()str.replace() allows us to swap characters by first replacing them with a temporary placeholder. This approach enables efficient swapping of commas and dots in a string without direct mapping.Example: Python s = "14, 625, 498.002" # Replace commas with a temporary character s = s.replace(',', '#') # Replace dots with commas s = s.replace('.', ',') # Replace the temporary character with dots s = s.replace('#', '.') print(s) Output14. 625. 498,002 Using regular expression re.sub() uses regular expressions for flexible text replacements. By applying a callback function, it efficiently swaps commas and dots based on their context.Example: Python import re s = "14, 625, 498.002" # Swap commas and dots with lambda function s = re.sub(r'[.,]', lambda x: ',' if x.group(0) == '.' else '.', s) print(s) Output14. 625. 498,002 Using List comprehensionList comprehension iterate over the string and swap commas and dots. The modified characters are then joined back into a string using join().Example: Python s = "14, 625, 498.002" # Swap commas and dots s = ''.join(['.' if char == ',' else ',' if char == '.' else char for char in s]) print(s) Output14. 625. 498,002 Using for LoopLoop iterate through the string and manually swap commas and dots. Characters are added to a list and then joined to form the final string.Example: Python s = "14, 625, 498.002" # Initialize empty list to store result res = [] for char in s: # If character is comma, append dot if char == ',': res.append('.') # If character is dot, append comma elif char == '.': res.append(',') # Otherwise, append the character as is else: res.append(char) # Join list of characters back into string s = ''.join(res) print(s) Output14. 625. 498,002 Advertise with us Next Article Split and Parse a string in Python C chinmoy lenka Follow Similar Reads Convert String with Comma To Float in Python When working with data in Python, it's not uncommon to encounter numeric values formatted with a mix of commas and dots as separators. Converting such strings to float is a common task, and Python offers several simple methods to achieve this. In this article, we will explore five generally used met 3 min read How to Change Values in a String in Python The task of changing values in a string in Python involves modifying specific parts of the string based on certain conditions. Since strings in Python are immutable, any modification requires creating a new string with the desired changes. For example, if we have a string like "Hello, World!", we mi 2 min read string.punctuation in Python In Python, the string module is a pre-initialized string used as a string constant. One such constant is string.punctuation, which provides a predefined string containing all the characters commonly considered punctuation.What is string.punctuation?string.punctuation is a string constant containing 3 min read Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is 2 min read Split and Parse a string in Python In this article, we'll look at different ways to split and parse strings in Python. Let's understand this with the help of a basic example:Pythons = "geeks,for,geeks" # Split the string by commas res = s.split(',') # Parse the list and print each element for item in res: print(item)Outputgeeks for g 2 min read Convert Lists to Comma-Separated Strings in Python Making a comma-separated string from a list of strings consists of combining the elements of the list into a single string with commas between each element. In this article, we will explore three different approaches to make a comma-separated string from a list of strings in Python. Make Comma-Separ 2 min read Article Tags : Python python-string Python string-programs Practice Tags : python Like