O
O
Old_TyT2021-12-19 06:24:17
Python
Old_TyT, 2021-12-19 06:24:17

How to add a line break?

There is a line with text, how to implement word wrapping to a new line with n characters, but the wrapping should not divide the word, i.e. wrap only on "spaces"
It should be something like this:

my_str = "Lorem Ipsum Lorem Ipsum"
perenos = 15
.
.
.
print(result)
Ожидаемый результат:
Lorem Ipsum\nLorem Ipsum

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Feor_slen, 2021-12-19
@Old_TyT

An inexperienced coder says that you can do this:

my_str = "Lorem Ipsum Lorem Ipsum"
perenos = 15
perenos1 = []
for i in my_str:
    perenos1.append(i)
perenos1.insert(perenos,'\n')
my_str = ''.join(perenos1)
print(my_str)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question