Python Forum

Full Version: word wrap text output
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my script has huge strings of text to output in a (probably virtual) text-mode environment. i would like to have something that can break up the text in word wrap or other appropriate mode. there a many more things to do than just split by spacing and form what fits. certain hyphenated words can be split at the hyphen, too. maybe there is a module around that does this right. in this case, something new to install on Xubuntu 18.04 is OK since it's just for me. but, a way to do this in the Python Library would still be a plus.
import pygame
pygame.init()
import textwrap
RED = (200, 20, 20)

screen = pygame.display.set_mode((400, 400))
your_text = """Bunch of text, blah, blah, and soforth.  Maybe a
story or something.  Who knows, doesn't matter"""

my_wrap = textwrap.TextWrapper(width = 25)
wrap_list = my_wrap.wrap(text=your_text)
y = 20
for line in wrap_list:
    myfont = pygame.font.SysFont(None,32) 
    mytext = myfont.render( line, 1, (RED))
    screen.blit(mytext, (0, y))
    y += 20
    
pygame.display.flip()