Python Practice › String Programs › Program 65

Swap commas and dots

EasyString Programs · 65 of 106

Write a Python program to swap commas and dots.

Advertisement
Input
"sqa,tools.python"
Output
sqa.tools,python

Approach

  1. translate with a two-way mapping swaps them in one pass.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More string programs

← All 106 String Programs