How to Swap Commas and Dots in a String in Python?

In this article, I will explain how to swap commas and dots in a string in Python. As a developer working on a project for my USA client, I came across a situation where I had a requirement to swap commas and dots in string. After researching, I found four efficient ways to accomplish this task. Let us learn more about this topic.

Swap Commas and Dots in a String in Python

Let us see the important methods to swap commas and dots in a string in Python.

Read How to Remove Punctuation from Strings in Python?

Method 1. Use str.replace()

This method is simple and effective for removing specific characters from a string by replacing them with an empty string in Python. You need to call replace() for each character you want to remove.

Example: Remove exclamation marks and question marks from a name.

name = "John! Doe?"
cleaned_name = name.replace('!', '').replace('?', '')
print(cleaned_name) 

Output:

John Doe

I have executed the above example code and added the screenshot below.

Swap Commas and Dots in a String in Python

str.replace() is simple but can be heavy if multiple characters need to be removed. It’s best for small-scale replacements where you know the characters to remove.

Check out How to Concatenate String and Int in Python?

Method 2. Use Regular Expression (re.sub())

Python regular expressions (regex) allow you to define patterns to match and replace characters. This method is very useful and efficient when working with complex patterns or multiple characters.

Example: Remove all non-alphanumeric characters except for spaces.

import re
name = "John! Doe@ 2024"
cleaned_name = re.sub(r'[^a-zA-Z0-9 ]', '', name)
print(cleaned_name) 

Output:

John Doe 2024

I have executed the above example code and added the screenshot below.

How to Swap Commas and Dots in a String in Python

re.sub() is highly efficient and flexible, especially when dealing with multiple or complex patterns. However, it can be harder to understand for beginners and might require some regex knowledge.

Read How to Convert Datetime to String in Python?

Method 3. Use List Comprehension

Python list comprehension offers a concise way to filter out unwanted characters. You can iterate through each character in the string and keep only the ones that meet the criteria.

Example: Remove characters that aren’t alphanumeric or spaces.

name = "John! Doe@ 2024"
cleaned_name = ''.join([char for char in name if char.isalnum() or char.isspace()])
print(cleaned_name) 

Output:

John Doe 2024

I have executed the above example code and added the screenshot below.

Swap Commas and Dots in a String in Python list comprehension

List comprehension is a clean and Pythonic approach, and it’s very readable. It’s best when you need to apply simple conditions, but can be less efficient for large datasets compared to regex.

Check out How to Repeat a String Multiple Times in Python?

Method 4. Use a for Loop

Python for loop method iterates over each character in the string and manually adds characters that meet the criteria to a new string.

Example: Remove characters that aren’t alphanumeric or spaces.

name = "John! Doe@ 2024"
cleaned_name = ''
for char in name:
    if char.isalnum() or char.isspace():
        cleaned_name += char
print(cleaned_name)  

Output:

John Doe 2024

Using a for loop is simple and flexible, but it can be less efficient than the list comprehension or regular expression methods. It’s useful for more custom processing.

Check out How to Split String by Whitespace in Python?

Conclusion

In this tutorial, I have explained how to swap commas and dots in a string in Python. I covered mainly four methods to accomplish this task they are str.replace(), regular expression(re.sub()), list comprehension, and for loop.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.