CopeAleph CopeAleph - 9 days ago 7
Python Question

Splitting messages in python

tfi = '''
aardvark
albatross
alligator
alpaca
ant
anteater
antelope
'''

print(tfi)


Ok so, this is just a small section of the tfi string, but how do I make the code print this out in sections decided by amount of characters.

pseudo:

print tfi (3 chars):
returns:
'aar'
(new message)
'dva'


and so on...

Answer

Try this:

def tfi(my_string, dif):
    for i in range(0, len(my_string), dif):
        print(my_string[i:i+dif])
Comments