Separate string in Python

Опубликовано: 25 Март 2026
на канале: Andres Niño
12,776
77

""" Separate string or text string, and join it with any character every certain number of characters, for this case I will separate a number with a period every three characters, from right to left. """

def dividirCadena(cadena, separador, numeroCaracteres):
cifra = ""
contador = 0
for numero in cadena[::-1]:
if contador == numeroCaracteres:
cifra += separador
contador = 0

contador += 1
cifra += numero

return (cifra[::-1], "".join(reversed(cifra)))

funcion = dividirCadena("189632019", ".", 3)
print(funcion)