Python Program to swap commas and dots in a string

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. 

Here's a Python program that swaps all commas and dots in a given string: 

 

def swap_commas_and_dots(string):
    # Create an empty result string
    result = ""

    # Loop through each character in the input string
    for char in string:
        # If the character is a comma, add a dot to the result string
        if char == ",":
            result += "."
        # If the character is a dot, add a comma to the result string
        elif char == ".":
            result += ","
        # Otherwise, add the character to the result string as is
        else:
            result += char

    return result

This program defines a function called swap_commas_and_dots that takes a single argument: a string that contains commas and dots. It then loops through each character in the input string, checking if each character is a comma or a dot. If it is a comma, a dot is added to the result string. If it is a dot, a comma is added to the result string. Otherwise, the character is added to the result string as is. Finally, the result string is returned.

Here's an example usage of this function: 

 

input_string = "This is a sentence, with commas, and dots. Let's swap them!"
output_string = swap_commas_and_dots(input_string)
print(output_string)

This would output the following string: 

 

This is a sentence. with commas. and dots, Let's swap them!

 

Submit Your Programming Assignment Details