How can I create a new line after a set of characters in python

Question:

I have the following code:

text = input('Enter your text: ')
if len(text) >= 16:
    text = text.replace(' ', 'n')
    print(f'Your new text is:n{text}')

If a user inputs blah blah blah blah the user would get the following output:

Your new text is:
blah
blah
blah
blah

I’m just wondering how can I make my code add a new line after the last available space before the 16 characters mark. So the output should end up being:

Your new text is:
blah blah blah
blah

I’m fairly new to python and I feel like I’m missing some sort of method that can get this done.

Asked By: Quessts

||

Answers:

text = input('Enter your text: ')
list_ = text.split(' ')

if len(text) >= 16:
   for i, j in zip(range(len(list_)),list_):
      if i == len(list_)-1:
         break
      print(j, end=' ')
   print('n'+ j)
Answered By: Davinder Singh

I would use rindex to search for the last occurrence of a space and then memorise the index position. As strings in Python are immutable I’d just form a new one with a n in the middle, like this:

text = input('Enter your text: ')
if len(text) >= 16:
     last_space_before_16 = text[:16].rindex(' ')
     text = text[:last_space_before_16] + 'n' + text[last_space_before_16+1:]
     print(f'Your new text is:n{text}')
Answered By: yvesonline

I can suggest a regex based approach here:

inp = "blah blah blah blah"
output = re.sub(r'^(.{1,16})s+', r'1n', inp)
print(output)

This prints:

blah blah blah
blah

The strategy here is to greedily match up to 16 characters, which are followed by breaking whitespace characters. Then, we replace by adding a newline character.

Answered By: Tim Biegeleisen

The reason that your original problem is happening is because text = text.replace(' ', 'n') will replace all of the spaces with a newline character.

Also, the current answers will not work if the user inputs a string that is very long – the 2nd print() could result in a line that is longer than 16 characters. For example:

>>> text = "blah " * 20
>>> print(text[:15])
blah blah blah 
>>> print(text[15:])
blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah 

For this to work on arbitrarily long user inputs, I’d suggest using a loop:

>>> text = "blah " * 20
>>> while len(text) > 16:
...     index = text[:16].rindex(' ') # finds the last space in the first 16 chars
...     print(text[:index].strip()) # strip() removes trailing spaces
...     text = text[index:] # Updates text to remove what we just printed
... 
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah
blah blah blah

>>> print(text.strip()) # Prints any remaining text after the loop
blah blah
Answered By: neal

You can limit it to the first 16 characters by using this code:

text = text[:15]
print(text)
Answered By: jaessom
import textwrap

text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vehicula lorem vitae quam commodo, at interdum lectus maximus."

wrapped_text = textwrap.fill(text, width=16)

print(wrapped_text)
Answered By: Daniil Mashkin
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.