![]() |
VOOZH | about |
In this article, we'll explore how to swap commas and dots in a string in Python.
str.translate() that allows for character substitution within a string using a translation table. It enables quick transformations, such as swapping characters like commas and dots.
Example:
14. 625. 498,002
Let's understand different methods to swap commas and dots in a string.
str.replace() allows us to swap characters by first replacing them with a temporary placeholder. This approach enables efficient swapping of commas and dots in a string without direct mapping.
Example:
14. 625. 498,002
re.sub() uses regular expressions for flexible text replacements. By applying a callback function, it efficiently swaps commas and dots based on their context.
Example:
14. 625. 498,002
List comprehension iterate over the string and swap commas and dots. The modified characters are then joined back into a string using join().
Example:
14. 625. 498,002
Loop iterate through the string and manually swap commas and dots. Characters are added to a list and then joined to form the final string.
Example:
14. 625. 498,002