Wrap Text in Python IDLE

Wrap Text in Python IDLE

[This answer applies to Python in general and is not specific to IDLE.]

For input of long strings without embedded newlines, you could enter a multiline string and then delete the newlines. But if you want lines to end with spaces after commas and periods, you will not see them and they will disappear if you strip line-ending whitespace from your code. (This is required for CPython stdlib code.)

An alternative is to use Pythons string literal concatenation feature. White space is guarded by line-ending quotes, and comments can be added. (See the link for another example.)

stories = {
    John : One day John went to the store to buy a game.    # the lead
        The name of the game was Super Blaster.    # the hint
        On the way to the store, John was blasted by a purple ray.  
        The ray of purple light, mixed with super neutrinos, 
        came from a alien spaceship hovering above.
    }

import textwrap
print(n.join(textwrap.wrap(stories[John])))

# prints

One day John went to the store to buy a game.  The name of the game
was Super Blaster.  On the way to the store, John was blasted by a
purple ray.  The ray of purple light, mixed with super neutrinos, came
from a alien spaceship hovering above.

Wrap Text in Python IDLE

Leave a Reply

Your email address will not be published. Required fields are marked *