For every string, I need to print # each 6 characters.
For example:
example_string = "this is an example string. ok ????"
myfunction(example_string)
"this i#s an e#xample# strin#g. ok #????"
What is the most efficient way to do that ?
How about this?
'#'.join( [example_string[a:a+6] for a in range(0,len(example_string),6)])
It runs pretty quickly, too. On my machine, five microseconds per 100-character string:
>>> import timeit
>>> timeit.Timer( "'#'.join([s[a:a+6] for a in range(0,len(s),6)])", "s='x'*100").timeit()
4.9556539058685303
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With