October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Swap Commas and Dots in a String in Python

Use Python's str.translate() and str.maketrans() to exchange commas and periods simultaneously, or str.replace() when you only need a one-way change.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use str.translate() with a table built by str.maketrans() to swap every comma and period at once. For example, "1,234.56" becomes "1.234,56", while all other characters stay unchanged.

Swap commas and periods simultaneously

Pair the two original characters with their replacements in one translation table:

s = "1,234.56"
swapped = s.translate(str.maketrans(",.", ".,"))

print(swapped)  # 1.234,56

str.maketrans() maps characters by position: the first character in the source string maps to the first in the destination, and the second maps to the second. The strings must have equal lengths. Then str.translate() applies that mapping to each character and returns a new string; it does not modify the original. See the Python 3.14.8 built-in types reference.

Change only one character with replace()

If you want to change every comma to a period but leave existing periods alone, use replace():

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
s = "1,234.56"
result = s.replace(",", ".")

print(result)  # 1.234.56

With no count argument, replace() changes every matching occurrence and returns a new string. To replace only the first two commas, for example, pass a count: s.replace(",", ".", 2). This is a one-way replacement, not a swap.

Why chained replacements do not swap safely

Calling replace() twice can change characters inserted by the first call. For instance:

s = "1,234.56"
result = s.replace(",", ".").replace(".", ",")

print(result)  # 1,234,56

The second call converts both the original period and the period inserted for the comma. Use the translation table for a simultaneous exchange, rather than relying on a temporary placeholder.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When the string represents a number

A character swap is a literal text operation, not a numeric parser or locale conversion. First establish what the input means: which mark is the decimal separator, which is a grouping separator, and whether the text follows a known convention. A blind swap can change the apparent value or create text that a decimal parser will not accept.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Python’s documented Decimal string syntax uses a period in the decimal part. The decimal module’s formatting recipe lets you configure the decimal point and grouping separator separately; formatting choices should be made for the intended output rather than by blindly exchanging characters. See the Python 3.14.8 decimal module reference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.