w3resource

Python: Swap comma and dot in a string

Python String: Exercise-48 with Solution

Write a Python program to swap commas and dots in a string.

Python String Exercises: Swap comma and dot in a string

Sample Solution:

Python Code:

# Define a string 'amount' with a numerical value in a specific format.
amount = "32.054,23"

# Create a variable 'maketrans' that references the 'maketrans' method of the 'amount' string.
maketrans = amount.maketrans

# Translate (replace) the characters ',' with '.', and '.' with ',' in the 'amount' string using the 'maketrans' variable.
amount = amount.translate(maketrans(',.', '.,'))

# Print the modified 'amount' string with the swapped decimal and comma characters.
print(amount) 

Sample Output:

32,054.23                  

Flowchart:

Flowchart: Swap comma and dot in a string

Python Code Editor:

Previous: Write a Python program to lowercase first n characters in a string.
Next: Write a Python program to count and display the vowels of a given text.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.