Home > Mobile >  Convert integer to string with dots inbetween every character
Convert integer to string with dots inbetween every character

Time:08-16

So I've got the following integer:

var = 100

This should be converted to:

var = "1.0.0"

What is the most pythonic way to do this?

CodePudding user response:

Use join

var = 100
var = ".".join(str(var))

CodePudding user response:

Your best bet is to convert to a string first, make it a list, add the decimal point to every list item except the last one, and then join the list. Something like this:

def strDecimal(num):

newStr = str(num)

newLst = list(newStr)

for index in range(len(newLst)):

if index != len(newLst) - 1:

  newLst[index] = newLst[index]   '.'

return ''.join(newLst)

  • Related