How can I make sure the word does not break in the middle? And leave a maximum of 40 characters per line?

4
valor = '''In the beginning God created the heavens and the earth. Now the 
earth was formless and empty, darkness was over the surface of the deep, and 
the Spirit of God was hovering over the waters.
And God said, "Let there be light," and there was light. God saw that the light 
was good, and he separated the light from the darkness. God called the light 
"day," and the darkness he called "night." And there was evening, and there was 
morning - the first day.'''

lista = valor
i = 0
f = 40
for c in lista:
    print(lista[i:f])
    i += 40
    f += 40
    
asked by anonymous 10.10.2018 / 15:49

1 answer

7

Use the textwrap module that comes with python:

import textwrap
for linha in textwrap.wrap(valor, 40):
    print(linha)

Result:

In the beginning God created the heavens
and the earth. Now the  earth was
formless and empty, darkness was over
the surface of the deep, and  the Spirit
of God was hovering over the waters. And
God said, "Let there be light," and
there was light. God saw that the light
was good, and he separated the light
from the darkness. God called the light
"day," and the darkness he called
"night." And there was evening, and
there was  morning - the first day.
    
10.10.2018 / 17:45