w3resource

Python: Swap comma and dot in a string

Python String: Exercise-48 with Solution

Swap commas and dots in a string.

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/string/python-data-type-string-exercise-48.php