Abisha Bindar
Abisha Bindar

Reputation: 43

Python || Creating new lines based on user input

I am wondering how you can let a given user input create new lines automatically. Like for instance:

summary = input("Please write a short summary")

Let the user input for example be:

An age-old vendetta between two powerful families erupts into bloodshed. A group of masked Montagues risk further conflict by gatecrashing a Capulet party. A young lovesick Romeo Montague falls instantly in love with Juliet Capulet, who is due to marry her father's choice, the County Paris.

Now I actually want this to be displayed as:

An age-old vendetta between two powerful families erupts into bloodshed.
A group of masked Montagues risk further conflict by gatecrashing a Capulet party.
A young lovesick Romeo Montague falls instantly in love with Juliet Capulet, who is due to marry her father's choice, the County Paris.

As you can see in the last example, it is creating new lines each time. I know you can create new lines with /n, but not how to import this into user inputs automatically, I hope you understand with the example given above.

Upvotes: 1

Views: 69

Answers (4)

eshirvana
eshirvana

Reputation: 24593

How about this :

print(summary.replace('. ','.\n'))

Output:

>An age-old vendetta between two powerful families erupts into bloodshed.
 A group of masked Montagues risk further conflict by gatecrashing a Capulet party.
 A young lovesick Romeo Montague falls instantly in love with Juliet Capulet, who is due to marry her father's choice, the County Paris.

Upvotes: 1

Keith
Keith

Reputation: 43024

Use the textwrap module.

import textwrap

print(textwrap.fill(myinput))

Upvotes: 2

TheEagle
TheEagle

Reputation: 5992

This strips unnecessary whitespaces, and preserves dots:

summary = input("Please write a short summary")
for line in summary.split("."):
    print(line.strip() + ".")

Upvotes: 1

Danish Bansal
Danish Bansal

Reputation: 700

The easiest way to print users long input in different lines is as below

inp = "An age-old vendetta between two powerful families erupts into bloodshed. A group of masked Montagues risk further conflict by gatecrashing a Capulet party. A young lovesick Romeo Montague falls instantly in love with Juliet Capulet, who is due to marry her father's choice, the County Paris."
for i in inp.split("."):
    print(i.strip())

inp is the input taken from user.

i.split(".") is splitting the input text based on dots(".").

i.strip() is simply removing extra space (if any) at start or at end of line.

Upvotes: 1

Related Questions