Home > Software design >  How to add new line after number of word
How to add new line after number of word

Time:09-10

i find a code to seperate line, but it cut hafl of word "A very very long str" \n "ing from user input". so i want to ask how to keep the original word. its mean add \n after a number of word?

# inp = "A very very long string from user input"
# new_input = ""
# for i, letter in enumerate(inp):
#     if i % 20 == 0:
#         new_input  = '\n'
#     new_input  = letter
#
# # this is just because at the beginning too a `\n` character gets added
# new_input = new_input[1:]
# print(new_input)

CodePudding user response:

Using a simple loop on a list of the words:

inp = "A very very long string from user input"

start = 0
N = 3
l = inp.split()
for stop in range(N, len(l) N, N):
    print(' '.join(l[start:stop]))
    start = stop

output:

A very very
long string from
user input

CodePudding user response:

Try using str.split() 1 and give it space as a delimiter:

words = inp.split(' ')

That should return a list of words

  • Related