Extract lines from custom paragraphs

I am getting user input using:

paragraphInput = input ("paste your paragraph ")
print(paragraphInput)

      

I get:

Line 1 of the paragraph
Line 2 of the paragraph
Line 3 of the paragraph
Line 4 of the paragraph

      

I would like to show something like this:

This is Line 1 of the paragraph !
This is Line 2 of the paragraph !
This is Line 3 of the paragraph !
This is Line 4 of the paragraph !

      

So, I wanted to use a loop for

, but I don't know how to get the line "n" of a paragraph and then add before This is

and after it !

. Is there a way to do this? Since the number of lines of paragraphs will change depending on the user ...

Thanks for taking the time to read and your help!

+3


source to share


2 answers


Python has a string splitting function.

https://docs.python.org/2/library/stdtypes.html#str.splitlines

For example, 'ab c\n\nde fg\rkl\r\n'.splitlines()

returns ['ab c', '', 'de fg', 'kl'],



Then you can simply iterate over that list, which now has matching line breaks.

for line in paragraphInput.splitlines():
    print "This is " + line + " !"

      

+3


source


You can do



for line in paragraphInput.split('\n'):
    print "This is " + line + " !"

      

+2


source







All Articles