0

What I mean by the title is how can I transform this:

    Example Example Example Example Example Example Example Example Example Exam
    ple Example

Into this:

    Example Example Example Example Example Example Example Example Example 
    Example Example

Any ideas?

C.Liddell
  • 1,084
  • 10
  • 19
  • 2
    Is this a string? Or python code? It's possible that the word wrapping is just a feature of the text editor you're using. – jayelm Feb 11 '14 at 18:15
  • In the interpreter? In a GUI? In the terminal window? In your editor? – jonrsharpe Feb 11 '14 at 18:20
  • I mean as in print "Example Example Example..." and wanted to wrap that in python Also I'm referring to in the IDLE. I'm sorry if I'm not being specific enough, I'm very new to this. – C.Liddell Feb 11 '14 at 18:33

2 Answers2

2

Use the textwrap module.

From the docs:

textwrap.wrap(text[, width[, ...]])

    Wraps the single paragraph in text (a string) so every
    line is at most width characters long. Returns a list
    of output lines, without final newlines.
Peter Westlake
  • 4,894
  • 1
  • 26
  • 35
  • So in this case I would do something like: textwrap.wrap("Example Example Example"[, 60[, 30]]) – C.Liddell Feb 11 '14 at 18:28
  • Yes, though without the square brackets, which are just part of the documentation. There are various other parameters you can pass. To wrap to a 20-character line: textwrap.wrap("Example Example Example", 20). – Peter Westlake Feb 11 '14 at 19:33
0

You would need to get the width of the thing you are printing to. (Probably the terminal). And then keep track of the length of string as you add the words. If the word would make python print it over two lines use a line break before it. This link is for getting terminal width How to get Linux console window width in Python

Community
  • 1
  • 1
namingFailed
  • 289
  • 4
  • 10