Since the strings are immutable, another way to do this is to turn the string into a list, which can then be indexed and modified without any clipping. However, to return the list to a string, you will need to use .join() using an empty string.
>>> hash = '355879ACB6' >>> hashlist = list(hash) >>> hashlist.insert(4, '-') >>> ''.join(hashlist) '3558-79ACB6'
I'm not sure how this compares to performance, but I feel it is easier on the eyes than other solutions .; -)
jathanism Mar 10 2018-11-11T00: 00Z
source share