Example
test = "123456789123"
I tried
test = "1234567"
print(".".join(test))
result
1.2.3.4.5.6.7
but I would like this
result
123.456.789.123
Example
test = "123456789123"
I tried
test = "1234567"
print(".".join(test))
result
1.2.3.4.5.6.7
but I would like this
result
123.456.789.123
Here is a simple regex solution:
import re
print(re.sub(r'(?<!^)(?=(\d{3})+$)', r'.', "12345673456456456"))
It produces the following output:
12.345.673.456.456.456
the regex uses lookahead to check that the number of digits after a given position is divisible by 3.
If you don't really need a dot, you can simply use:
formated_number = "{:,}".format(value)
And if you really want those dots:
formated_number.replace(',','.')
Very naive solution without list comprehension:
test = '123456789123'
result = ''
while test:
result += test[:3]
if len(test) > 3:
result += '.'
test = test[3:]
print(result)
You can loop over the characters as a list and track where you are with a counter:
s = "123456789123"
output = []
count = 0
for c in list(s):
count += 1
if count == 4:
output.append(".")
count = 0
continue
else:
output.append(c)
result = ''.join(output)
print(result)
A straightforward implementation:
test = "1234567891234431222334442234ee3432"
testJoined = ""
for i in range(0,len(test),3):
testJoined += test[i:i+3] + "."
print(testJoined[0:-1])
test1:
"1234567891234431222334442234ee3432"
result1:
123.456.789.123.443.122.233.444.223.4ee.343.2
test2:
"1234567891234431222334442234ee34aa32"
result2:
123.456.789.123.443.122.233.444.223.4ee.34a.a32