Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print string in this way

Tags:

python

regex

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 ?

like image 627
xRobot Avatar asked Dec 07 '22 02:12

xRobot


1 Answers

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
like image 159
ʇsәɹoɈ Avatar answered Dec 26 '22 08:12

ʇsәɹoɈ