Reputation: 11
I have an id number (idnum)that is equal to 12345678. And is needs to be formatted to look like 123-45-678, which is equal to (new).
newid = (int(idnum[:3]) + "-" + (int(idnum[4:6])) + "-" + (int(idnum[7:9]))
That is what I'm typing but its not working. The error I'm getting is "int not subscriptable."
Upvotes: 1
Views: 2456
Reputation: 28
You can't split an integer into its digits while maintaining it as an integer.
Your best option is to convert your number to a string and then manipulate it accordingly.
i.e.
idnum_str = repr(idnum) #number as string
newid = idnum_str[:3] + "-" + idnum_str[4:6] + "-" + idnum_str[7:9]
print(newid)
Upvotes: 1