Gissipi_453
Gissipi_453

Reputation: 1340

Give double line breaks after certain number of full stops to make String divided into paragraphs

I have a long string in Python

longString = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like)."

I want to give double new lines after certain number of full stops to make the String divided into paragraphs.

I tried textwrap, but not sure if textwrap does it. Any idea how this can be achieved?

Expected output is something like this:

It is a long established fact that a reader will be distracted by the
readable content of a page when looking at its layout.

The point of using Lorem Ipsum is that it has a more-or-less normal
distribution of letters, as opposed to using 'Content here, content
here', making it look like readable English.

Many desktop publishing packages and web page editors now use Lorem
Ipsum as their default model text, and a search for 'lorem ipsum' will
uncover many web sites still in their infancy.

Various versions have evolved over the years, sometimes by accident,
sometimes on purpose (injected humour and the like).

Upvotes: 0

Views: 49

Answers (2)

Mark
Mark

Reputation: 81

If the string data has to be in the code I would recommend using a multi-line string, we need to use textwrap.dedent too in case the multi-line string causes indentation. We can then remove the newlines that we don't want, and then we can replace the full stops with a full stop + newline.

import textwrap

longString = """It is a long established fact that a reader will be 
distracted by the readable content of a page when looking at its layout. 
The point of using Lorem Ipsum is that it has a more-or-less normal 
distribution of letters, as opposed to using 'Content here, content 
here', making it look like readable English. Many desktop publishing 
packages and web page editors now use Lorem Ipsum as their default model 
text, and a search for 'lorem ipsum' will uncover many web sites still 
in their infancy. Various versions have evolved over the years, 
sometimes by accident, sometimes on purpose (injected humour and the 
like)."""

longString = textwrap.dedent(longString)
longString = longString.replace('\n', '')
longString = longString.replace('. ', '.\n\n')
print(longString)

You could also opt to put the string data in a separate text file since the string is quite long, which means you can avoid a lot more string manipulation while also maintaining clean code.

myfile = open('file.txt', 'r')
longString = myfile.read().replace('. ', '.\n\n')
print(longString)

Upvotes: 1

Memegamer138
Memegamer138

Reputation: 36

Try the following code:

`s = '''It is a long established fact that a reader will be distracted by the readable 
content of a page when looking at its layout. The point of using Lorem Ipsum is that 
it has a more-or-less normal distribution of letters, as opposed to using 'Content 
here, content here', making it look like readable English. Many desktop publishing 
packages and web page editors now use Lorem Ipsum as their default model text, and a 
search for 'lorem ipsum' will uncover many web sites still in their infancy. Various 
versions have evolved over the years, sometimes by accident, sometimes on purpose 
(injected humour and the like)'''

a = ''
for i in s:
    if i=='.':
        a+=i
        a+=' \n\n'
    else:
        a+=i
print(a)`

If you executed the code, you would've realized that the output isn't exactly what you need as there is a blank space in the beginning of every line. This is because of the string you entered. The blank space after the periods are making the lines appear 1 space further from the edge. So please remove the spaces after the periods and you will get the desired output. If there is a way to get the output you need without removing the spaces after the periods in the string, please let me know.

Upvotes: 1

Related Questions