Home > other >  How do I make the string linebreak when there is a period?
How do I make the string linebreak when there is a period?

Time:07-22

I have this string and I want the output to newline the string, every time there is a period. Can I use string methods for this?

string = """
Electronic mail (email or e-mail) is a method of exchanging 
messages ("mail") between people using electronic devices. Email was thus 
conceived as the electronic (digital) version of, or counterpart to, mail, 
at a time when "mail" meant only physical mail (hence e-   mail). Email 
later became a ubiquitous (very widely used) communication medium, to 
the point that in current use, an e-mail address is often treated as a basic
and necessary part of many processes in business, commerce, government, 
education, entertainment, and other spheres of daily life in most 
countries. Email is the medium, and each message sent therewith is 
called an email (mass/count distinction).
"""

CodePudding user response:

If you want all the line breaks to only be at a period

string = string.replace('\n', '').replace('. ', '.\n')
print(string)

CodePudding user response:

Yes! The String class has a function "replace" which you could use:

string.replace('.', '.\n')

This will return a copy of "string" with all periods changed to periods followed by newlines.

  • Related