How to make Python start a new line rather than cutting words off

Viewed 73

I'm just using a simple print() function, but when the output hits the end of the window, rather than taking the last word and using it to start a new line, it just continues it in the next line so I have half of the word in one line and the other half in the next line.
For example:

print('One of my favourite things to do over the holiday is go snowboarding.')

Output:
One of my favourite things to do over t| < this would be the end of the window
he holiday is to go snowboarding.

4 Answers

You can use textwrap module

import textwrap
strs = "One of my favourite things to do over the holiday is go snowboarding"
print(textwrap.fill(strs, 20))

Output: One of my favourite
things to do over
the holiday is go
snowboarding

You could write a little alternative print function for this:

def print2(text, line_len_limit):
    if len(text) <= line_len_limit:
        print(text)
    else:
        print(text[:line_len_limit])
        return(print2(text[line_len_limit:], line_len_limit)) #recursive call

text = 'One of my favourite things to do over the holiday is go snowboarding.'        
print2(text, 10)

Output:

One of my 
favourite 
things to 
do over th
e holiday 
is go snow
boarding.

If you want to avoid cutting words, then:

def print3(text, line_len_limit):
    words = text.split(' ')    
    line = ''
    for i, word in enumerate(words):
        if len(line) + len(word) < line_len_limit:
            line += f'{word} '
        else:
            print(line)
            if len(word) >= line_len_limit:
                print(word)
                return(print3(' '.join(words[i+1:]), line_len_limit))
            return(print3(' '.join(words[i:]), line_len_limit))

print3(text, 10)

Output:

One of my 
favourite 
things to 
do over 
the 
holiday 
is go 
snowboarding.

You can use https://docs.python.org/3/library/shutil.html#shutil.get_terminal_size to query the number of columns in the terminal. It falls back to having 80 columns when python is not attached to a terminal. You can provide this column size as the width arg to https://docs.python.org/3/library/textwrap.html#textwrap.fill . textwrap.fill() will inject newlines into the string such that no line is longer than the given width, and will only break up words if they are exceptionally long.

import shutil
import textwrap

output = (
    'One of my favourite things to do over the holiday is go snowboarding. '
    'Another sentence to make the string length over 80 characters.'
)

print(textwrap.fill(output, width=shutil.get_terminal_size().columns))

Depending on your terminal size, this will produce something like:

One of my favourite things to do over the holiday is go snowboarding. Another
sentence to make the string length over 80 characters.

you can use new line:

print('One of my favourite things to do over\nthe holiday is go snowboarding.') note \n within your printed text

or

print("One of my favourite things to do over"+"\n"+"the holiday is go snowboarding.")

Related