Python Practice › String Programs › Program 65
Swap commas and dots
Write a Python program to swap commas and dots.
Input
"sqa,tools.python"
Output
sqa.tools,python
Approach
translatewith a two-way mapping swaps them in one pass.
Solution
text = "sqa,tools.python"print(text.translate(str.maketrans(",.", ".,")))Output
sqa.tools,python
Line by line
Line 2
maketrans(",.", ".,") maps , to . and . to , at the same time; two replace calls would undo each other.
Learn the concept
New to this topic? Read the lesson, then come back.